00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 abstract class Mage_Catalog_Model_Product_Type_Abstract
00035 {
00036
00037
00038
00039
00040
00041
00042 protected $_product;
00043
00044
00045
00046
00047
00048
00049 protected $_typeId;
00050
00051
00052
00053
00054
00055
00056 protected $_setAttributes;
00057
00058
00059
00060
00061
00062
00063 protected $_editableAttributes;
00064
00065
00066
00067
00068
00069
00070 protected $_isComposite = false;
00071
00072
00073
00074
00075
00076
00077 protected $_storeFilter = null;
00078
00079 const CALCULATE_CHILD = 0;
00080 const CALCULATE_PARENT = 1;
00081
00082
00083
00084
00085
00086 const SHIPMENT_SEPARATELY = 1;
00087 const SHIPMENT_TOGETHER = 0;
00088
00089
00090
00091
00092
00093
00094
00095 public function setProduct($product)
00096 {
00097 $this->_product = $product;
00098 return $this;
00099 }
00100
00101
00102
00103
00104
00105
00106
00107 public function setTypeId($typeId)
00108 {
00109 $this->_typeId = $typeId;
00110 return $this;
00111 }
00112
00113
00114
00115
00116
00117
00118
00119 public function getProduct($product = null)
00120 {
00121 if (is_object($product)) {
00122 return $product;
00123 }
00124 return $this->_product;
00125 }
00126
00127
00128
00129
00130
00131
00132 public function getRelationInfo()
00133 {
00134 return new Varien_Object();
00135 }
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147 public function getChildrenIds($parentId, $required = true)
00148 {
00149 return array();
00150 }
00151
00152
00153
00154
00155
00156
00157
00158 public function getParentIdsByChild($childId)
00159 {
00160 return array();
00161 }
00162
00163
00164
00165
00166
00167
00168
00169 public function getSetAttributes($product = null)
00170 {
00171 return $this->getProduct($product)->getResource()
00172 ->loadAllAttributes($this->getProduct($product))
00173 ->getSortedAttributes($this->getProduct($product)->getAttributeSetId());
00174 }
00175
00176
00177
00178
00179
00180
00181
00182
00183 public function attributesCompare($attribute1, $attribute2)
00184 {
00185 $sort1 = ($attribute1->getGroupSortPath() * 1000) + ($attribute1->getSortPath() * 0.0001);
00186 $sort2 = ($attribute2->getGroupSortPath() * 1000) + ($attribute2->getSortPath() * 0.0001);
00187
00188 if ($sort1 > $sort2) {
00189 return 1;
00190 } elseif ($sort1 < $sort2) {
00191 return -1;
00192 }
00193
00194 return 0;
00195 }
00196
00197
00198
00199
00200
00201
00202
00203 public function getEditableAttributes($product = null)
00204 {
00205 $cacheKey = '_cache_editable_attributes';
00206 if (!$this->getProduct($product)->hasData($cacheKey)) {
00207 $editableAttributes = array();
00208 foreach ($this->getSetAttributes($product) as $attributeCode => $attribute) {
00209 if (!is_array($attribute->getApplyTo())
00210 || count($attribute->getApplyTo())==0
00211 || in_array($this->getProduct($product)->getTypeId(), $attribute->getApplyTo())) {
00212 $editableAttributes[$attributeCode] = $attribute;
00213 }
00214 }
00215 $this->getProduct($product)->setData($cacheKey, $editableAttributes);
00216 }
00217 return $this->getProduct($product)->getData($cacheKey);
00218 }
00219
00220
00221
00222
00223
00224
00225
00226 public function getAttributeById($attributeId, $product = null)
00227 {
00228 foreach ($this->getSetAttributes($product) as $attribute) {
00229 if ($attribute->getId() == $attributeId) {
00230 return $attribute;
00231 }
00232 }
00233 return null;
00234 }
00235
00236
00237
00238
00239
00240
00241
00242 public function isVirtual($product = null)
00243 {
00244 return false;
00245 }
00246
00247
00248
00249
00250
00251
00252
00253 public function isSalable($product = null)
00254 {
00255 $salable = $this->getProduct($product)->getStatus() == Mage_Catalog_Model_Product_Status::STATUS_ENABLED;
00256 if ($salable && $this->getProduct($product)->hasData('is_salable')) {
00257 $salable = $this->getProduct($product)->getData('is_salable');
00258 }
00259 elseif ($salable && $this->isComposite()) {
00260 $salable = null;
00261 }
00262
00263 return $salable;
00264 }
00265
00266
00267
00268
00269
00270
00271
00272
00273 public function prepareForCart(Varien_Object $buyRequest, $product = null)
00274 {
00275 $product = $this->getProduct($product);
00276
00277
00278 try {
00279 $options = $this->_prepareOptionsForCart($buyRequest, $product);
00280 } catch (Mage_Core_Exception $e) {
00281 return $e->getMessage();
00282 }
00283
00284 if (is_string($options)) {
00285 return $options;
00286 }
00287
00288
00289 $superProductConfig = $buyRequest->getSuperProductConfig();
00290 if (!empty($superProductConfig['product_id'])
00291 && !empty($superProductConfig['product_type'])) {
00292 $superProductId = (int) $superProductConfig['product_id'];
00293 if ($superProductId) {
00294 if (!$superProduct = Mage::registry('used_super_product_'.$superProductId)) {
00295 $superProduct = Mage::getModel('catalog/product')->load($superProductId);
00296 Mage::register('used_super_product_'.$superProductId, $superProduct);
00297 }
00298 if ($superProduct->getId()) {
00299 $assocProductIds = $superProduct->getTypeInstance(true)->getAssociatedProductIds($superProduct);
00300 if (in_array($product->getId(), $assocProductIds)) {
00301 $productType = $superProductConfig['product_type'];
00302 $product->addCustomOption('product_type', $productType, $superProduct);
00303
00304 $buyRequest->setData('super_product_config', array(
00305 'product_type' => $productType,
00306 'product_id' => $superProduct->getId()
00307 )
00308 );
00309 }
00310 }
00311 }
00312 }
00313
00314 $product->addCustomOption('info_buyRequest', serialize($buyRequest->getData()));
00315
00316 if ($options) {
00317 $optionIds = array_keys($options);
00318 $product->addCustomOption('option_ids', implode(',', $optionIds));
00319 foreach ($options as $optionId => $optionValue) {
00320 $product->addCustomOption('option_'.$optionId, $optionValue);
00321 }
00322 }
00323
00324 $product->setCartQty($buyRequest->getQty());
00325
00326 return array($product);
00327 }
00328
00329
00330
00331
00332
00333
00334 public function getSpecifyOptionMessage()
00335 {
00336 return Mage::helper('catalog')->__('Please specify the product required option(s)');
00337 }
00338
00339
00340
00341
00342
00343
00344
00345
00346 protected function _prepareOptionsForCart(Varien_Object $buyRequest, $product = null)
00347 {
00348 $newOptions = array();
00349 foreach ($this->getProduct($product)->getOptions() as $_option) {
00350
00351 $group = $_option->groupFactory($_option->getType())
00352 ->setOption($_option)
00353 ->setProduct($this->getProduct($product))
00354 ->setRequest($buyRequest)
00355 ->validateUserValue($buyRequest->getOptions());
00356
00357 $preparedValue = $group->prepareForCart();
00358 if ($preparedValue !== null) {
00359 $newOptions[$_option->getId()] = $preparedValue;
00360 }
00361 }
00362 return $newOptions;
00363 }
00364
00365
00366
00367
00368
00369
00370
00371
00372 public function checkProductBuyState($product = null)
00373 {
00374 if (!$this->getProduct($product)->getSkipCheckRequiredOption()) {
00375 foreach ($this->getProduct($product)->getOptions() as $option) {
00376 if ($option->getIsRequire() && (!$this->getProduct($product)->getCustomOption('option_'.$option->getId())
00377 || strlen($this->getProduct($product)->getCustomOption('option_'.$option->getId())->getValue()) == 0)) {
00378 Mage::throwException(
00379 Mage::helper('catalog')->__('Product has required options')
00380 );
00381 break;
00382 }
00383 }
00384 }
00385
00386 return $this;
00387 }
00388
00389
00390
00391
00392
00393
00394
00395
00396 public function getOrderOptions($product = null)
00397 {
00398 $optionArr = array();
00399 if ($info = $this->getProduct($product)->getCustomOption('info_buyRequest')) {
00400 $optionArr['info_buyRequest'] = unserialize($info->getValue());
00401 }
00402
00403 if ($optionIds = $this->getProduct($product)->getCustomOption('option_ids')) {
00404 foreach (explode(',', $optionIds->getValue()) as $optionId) {
00405 if ($option = $this->getProduct($product)->getOptionById($optionId)) {
00406
00407 $quoteItemOption = $this->getProduct($product)->getCustomOption('option_'.$option->getId());
00408
00409 $group = $option->groupFactory($option->getType())
00410 ->setOption($option)
00411 ->setProduct($this->getProduct())
00412 ->setQuoteItemOption($quoteItemOption);
00413
00414 $optionArr['options'][] = array(
00415 'label' => $option->getTitle(),
00416 'value' => $group->getFormattedOptionValue($quoteItemOption->getValue()),
00417 'print_value' => $group->getPrintableOptionValue($quoteItemOption->getValue()),
00418 'option_id' => $option->getId(),
00419 'option_type' => $option->getType(),
00420 'option_value' => $quoteItemOption->getValue(),
00421 'custom_view' => $group->isCustomizedView()
00422 );
00423 }
00424 }
00425 }
00426
00427 if ($productTypeConfig = $this->getProduct($product)->getCustomOption('product_type')) {
00428 $optionArr['super_product_config'] = array(
00429 'product_code' => $productTypeConfig->getCode(),
00430 'product_type' => $productTypeConfig->getValue(),
00431 'product_id' => $productTypeConfig->getProductId()
00432 );
00433 }
00434
00435 return $optionArr;
00436 }
00437
00438
00439
00440
00441
00442
00443
00444 public function save($product = null)
00445 {
00446 return $this;
00447 }
00448
00449
00450
00451
00452
00453
00454
00455 public function beforeSave($product = null)
00456 {
00457 $this->getProduct($product)->canAffectOptions(true);
00458 return $this;
00459 }
00460
00461
00462
00463
00464
00465
00466
00467 public function isComposite($product = null)
00468 {
00469 return $this->_isComposite;
00470 }
00471
00472
00473
00474
00475
00476
00477
00478 public function getSku($product = null)
00479 {
00480 $skuDelimiter = '-';
00481 $sku = $this->getProduct($product)->getData('sku');
00482 if ($optionIds = $this->getProduct($product)->getCustomOption('option_ids')) {
00483 foreach (explode(',', $optionIds->getValue()) as $optionId) {
00484 if ($option = $this->getProduct($product)->getOptionById($optionId)) {
00485
00486 $quoteItemOption = $this->getProduct($product)->getCustomOption('option_'.$optionId);
00487
00488 $group = $option->groupFactory($option->getType())
00489 ->setOption($option);
00490
00491 if ($optionSku = $group->getOptionSku($quoteItemOption->getValue(), $skuDelimiter)) {
00492 $sku .= $skuDelimiter . $optionSku;
00493 }
00494 }
00495 }
00496 }
00497 return $sku;
00498 }
00499
00500
00501
00502
00503
00504
00505
00506 public function getWeight($product = null)
00507 {
00508 return $this->getProduct($product)->getData('weight');
00509 }
00510
00511
00512
00513
00514
00515
00516
00517 public function hasOptions($product = null)
00518 {
00519 if ($this->getProduct($product)->getHasOptions()) {
00520 return true;
00521 }
00522 return false;
00523 }
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537 public function updateQtyOption($options, Varien_Object $option, $value, $product = null)
00538 {
00539 return $this;
00540 }
00541
00542
00543
00544
00545
00546
00547
00548 public function hasRequiredOptions($product = null)
00549 {
00550 if ($this->getProduct($product)->getRequiredOptions()) {
00551 return true;
00552 }
00553 return false;
00554 }
00555
00556
00557
00558
00559
00560
00561 public function getStoreFilter($product = null)
00562 {
00563 $cacheKey = '_cache_instance_store_filter';
00564 return $this->getProduct($product)->getData($cacheKey);
00565 }
00566
00567
00568
00569
00570
00571
00572
00573 public function setStoreFilter($store=null, $product = null)
00574 {
00575 $cacheKey = '_cache_instance_store_filter';
00576 $this->getProduct($product)->setData($cacheKey, $store);
00577 return $this;
00578 }
00579
00580
00581
00582
00583
00584
00585
00586 public function getForceChildItemQtyChanges($product = null)
00587 {
00588 return false;
00589 }
00590
00591
00592
00593
00594
00595
00596
00597 public function prepareQuoteItemQty($qty, $product = null)
00598 {
00599 return floatval($qty);
00600 }
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611 public function assignProductToOption($optionProduct, $option, $product = null)
00612 {
00613 if ($optionProduct) {
00614 $option->setProduct($optionProduct);
00615 } else {
00616 $option->setProduct($this->getProduct($product));
00617 }
00618
00619 return $this;
00620 }
00621
00622
00623
00624
00625
00626
00627
00628 public function setConfig($config)
00629 {
00630 if (isset($config['composite'])) {
00631 $this->_isComposite = (bool) $config['composite'];
00632 }
00633 return $this;
00634 }
00635
00636
00637
00638
00639
00640
00641
00642
00643 public function getSearchableData($product = null)
00644 {
00645 $product = $this->getProduct($product);
00646
00647 $searchData = Mage::getSingleton('catalog/product_option')
00648 ->getSearchableData($product->getId(), $product->getStoreId());
00649
00650 return $searchData;
00651 }
00652 }