PageRenderTime 42ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Catalog/Model/Product/Type/Configurable.php

https://bitbucket.org/andrewjleavitt/magestudy
PHP | 795 lines | 436 code | 71 blank | 288 comment | 67 complexity | 480986ad53dc5bc0921ae5b086ec4396 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Catalog
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Configurable product type implementation
  28. *
  29. * This type builds in product attributes and existing simple products
  30. *
  31. * @category Mage
  32. * @package Mage_Catalog
  33. * @author Magento Core Team <core@magentocommerce.com>
  34. */
  35. class Mage_Catalog_Model_Product_Type_Configurable extends Mage_Catalog_Model_Product_Type_Abstract
  36. {
  37. const TYPE_CODE = 'configurable';
  38. /**
  39. * Cache key for Used Product Attribute Ids
  40. *
  41. * @var string
  42. */
  43. protected $_usedProductAttributeIds = '_cache_instance_used_product_attribute_ids';
  44. /**
  45. * Cache key for Used Product Attributes
  46. *
  47. * @var string
  48. */
  49. protected $_usedProductAttributes = '_cache_instance_used_product_attributes';
  50. /**
  51. * Cache key for Used Attributes
  52. *
  53. * @var string
  54. */
  55. protected $_usedAttributes = '_cache_instance_used_attributes';
  56. /**
  57. * Cache key for configurable attributes
  58. *
  59. * @var string
  60. */
  61. protected $_configurableAttributes = '_cache_instance_configurable_attributes';
  62. /**
  63. * Cache key for Used product ids
  64. *
  65. * @var string
  66. */
  67. protected $_usedProductIds = '_cache_instance_product_ids';
  68. /**
  69. * Cache key for used products
  70. *
  71. * @var string
  72. */
  73. protected $_usedProducts = '_cache_instance_products';
  74. /**
  75. * Product is composite
  76. *
  77. * @var bool
  78. */
  79. protected $_isComposite = true;
  80. /**
  81. * Product is configurable
  82. *
  83. * @var bool
  84. */
  85. protected $_canConfigure = true;
  86. /**
  87. * Return relation info about used products
  88. *
  89. * @return Varien_Object Object with information data
  90. */
  91. public function getRelationInfo()
  92. {
  93. $info = new Varien_Object();
  94. $info->setTable('catalog/product_super_link')
  95. ->setParentFieldName('parent_id')
  96. ->setChildFieldName('product_id');
  97. return $info;
  98. }
  99. /**
  100. * Retrieve Required children ids
  101. * Return grouped array, ex array(
  102. * group => array(ids)
  103. * )
  104. *
  105. * @param int $parentId
  106. * @param bool $required
  107. * @return array
  108. */
  109. public function getChildrenIds($parentId, $required = true)
  110. {
  111. return Mage::getResourceSingleton('catalog/product_type_configurable')
  112. ->getChildrenIds($parentId, $required);
  113. }
  114. /**
  115. * Retrieve parent ids array by requered child
  116. *
  117. * @param int|array $childId
  118. * @return array
  119. */
  120. public function getParentIdsByChild($childId)
  121. {
  122. return Mage::getResourceSingleton('catalog/product_type_configurable')
  123. ->getParentIdsByChild($childId);
  124. }
  125. /**
  126. * Retrieve product type attributes
  127. *
  128. * @param Mage_Catalog_Model_Product $product
  129. * @return array
  130. */
  131. public function getEditableAttributes($product = null)
  132. {
  133. if (is_null($this->_editableAttributes)) {
  134. $this->_editableAttributes = parent::getEditableAttributes($product);
  135. foreach ($this->_editableAttributes as $index => $attribute) {
  136. if ($this->getUsedProductAttributeIds($product)
  137. && in_array($attribute->getAttributeId(), $this->getUsedProductAttributeIds($product))) {
  138. unset($this->_editableAttributes[$index]);
  139. }
  140. }
  141. }
  142. return $this->_editableAttributes;
  143. }
  144. /**
  145. * Checkin attribute availability for create superproduct
  146. *
  147. * @param Mage_Eav_Model_Entity_Attribute $attribute
  148. * @return bool
  149. */
  150. public function canUseAttribute(Mage_Eav_Model_Entity_Attribute $attribute)
  151. {
  152. $allow = $attribute->getIsGlobal() == Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL
  153. && $attribute->getIsVisible()
  154. && $attribute->getIsConfigurable()
  155. && $attribute->usesSource()
  156. && $attribute->getIsUserDefined();
  157. return $allow;
  158. }
  159. /**
  160. * Declare attribute identifiers used for asign subproducts
  161. *
  162. * @param array $ids
  163. * @param Mage_Catalog_Model_Product $product
  164. * @return Mage_Catalog_Model_Product_Type_Configurable
  165. */
  166. public function setUsedProductAttributeIds($ids, $product = null)
  167. {
  168. $usedProductAttributes = array();
  169. $configurableAttributes = array();
  170. foreach ($ids as $attributeId) {
  171. $usedProductAttributes[] = $this->getAttributeById($attributeId);
  172. $configurableAttributes[] = Mage::getModel('catalog/product_type_configurable_attribute')
  173. ->setProductAttribute($this->getAttributeById($attributeId));
  174. }
  175. $this->getProduct($product)->setData($this->_usedProductAttributes, $usedProductAttributes);
  176. $this->getProduct($product)->setData($this->_usedProductAttributeIds, $ids);
  177. $this->getProduct($product)->setData($this->_configurableAttributes, $configurableAttributes);
  178. return $this;
  179. }
  180. /**
  181. * Retrieve identifiers of used product attributes
  182. *
  183. * @param Mage_Catalog_Model_Product $product
  184. * @return array
  185. */
  186. public function getUsedProductAttributeIds($product = null)
  187. {
  188. if (!$this->getProduct($product)->hasData($this->_usedProductAttributeIds)) {
  189. $usedProductAttributeIds = array();
  190. foreach ($this->getUsedProductAttributes($product) as $attribute) {
  191. $usedProductAttributeIds[] = $attribute->getId();
  192. }
  193. $this->getProduct($product)->setData($this->_usedProductAttributeIds, $usedProductAttributeIds);
  194. }
  195. return $this->getProduct($product)->getData($this->_usedProductAttributeIds);
  196. }
  197. /**
  198. * Retrieve used product attributes
  199. *
  200. * @param Mage_Catalog_Model_Product $product
  201. * @return array
  202. */
  203. public function getUsedProductAttributes($product = null)
  204. {
  205. if (!$this->getProduct($product)->hasData($this->_usedProductAttributes)) {
  206. $usedProductAttributes = array();
  207. $usedAttributes = array();
  208. foreach ($this->getConfigurableAttributes($product) as $attribute) {
  209. $id = $attribute->getProductAttribute()->getId();
  210. $usedProductAttributes[$id] = $attribute->getProductAttribute();
  211. $usedAttributes[$id] = $attribute;
  212. }
  213. $this->getProduct($product)->setData($this->_usedAttributes, $usedAttributes);
  214. $this->getProduct($product)->setData($this->_usedProductAttributes, $usedProductAttributes);
  215. }
  216. return $this->getProduct($product)->getData($this->_usedProductAttributes);
  217. }
  218. /**
  219. * Retrieve configurable attrbutes data
  220. *
  221. * @param Mage_Catalog_Model_Product $product
  222. * @return array
  223. */
  224. public function getConfigurableAttributes($product = null)
  225. {
  226. Varien_Profiler::start('CONFIGURABLE:'.__METHOD__);
  227. if (!$this->getProduct($product)->hasData($this->_configurableAttributes)) {
  228. $configurableAttributes = $this->getConfigurableAttributeCollection($product)
  229. ->orderByPosition()
  230. ->load();
  231. $this->getProduct($product)->setData($this->_configurableAttributes, $configurableAttributes);
  232. }
  233. Varien_Profiler::stop('CONFIGURABLE:'.__METHOD__);
  234. return $this->getProduct($product)->getData($this->_configurableAttributes);
  235. }
  236. /**
  237. * Retrieve Configurable Attributes as array
  238. *
  239. * @param Mage_Catalog_Model_Product $product
  240. * @return array
  241. */
  242. public function getConfigurableAttributesAsArray($product = null)
  243. {
  244. $res = array();
  245. foreach ($this->getConfigurableAttributes($product) as $attribute) {
  246. $res[] = array(
  247. 'id' => $attribute->getId(),
  248. 'label' => $attribute->getLabel(),
  249. 'use_default' => $attribute->getUseDefault(),
  250. 'position' => $attribute->getPosition(),
  251. 'values' => $attribute->getPrices() ? $attribute->getPrices() : array(),
  252. 'attribute_id' => $attribute->getProductAttribute()->getId(),
  253. 'attribute_code' => $attribute->getProductAttribute()->getAttributeCode(),
  254. 'frontend_label' => $attribute->getProductAttribute()->getFrontend()->getLabel(),
  255. 'store_label' => $attribute->getProductAttribute()->getStoreLabel(),
  256. );
  257. }
  258. return $res;
  259. }
  260. /**
  261. * Retrieve configurable atrribute collection
  262. *
  263. * @param Mage_Catalog_Model_Product $product
  264. * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Type_Configurable_Attribute_Collection
  265. */
  266. public function getConfigurableAttributeCollection($product = null)
  267. {
  268. return Mage::getResourceModel('catalog/product_type_configurable_attribute_collection')
  269. ->setProductFilter($this->getProduct($product));
  270. }
  271. /**
  272. * Retrieve subproducts identifiers
  273. *
  274. * @param Mage_Catalog_Model_Product $product
  275. * @return array
  276. */
  277. public function getUsedProductIds($product = null)
  278. {
  279. if (!$this->getProduct($product)->hasData($this->_usedProductIds)) {
  280. $usedProductIds = array();
  281. foreach ($this->getUsedProducts(null, $product) as $product) {
  282. $usedProductIds[] = $product->getId();
  283. }
  284. $this->getProduct($product)->setData($this->_usedProductIds, $usedProductIds);
  285. }
  286. return $this->getProduct($product)->getData($this->_usedProductIds);
  287. }
  288. /**
  289. * Retrieve array of "subproducts"
  290. *
  291. * @param array
  292. * @param Mage_Catalog_Model_Product $product
  293. * @return array
  294. */
  295. public function getUsedProducts($requiredAttributeIds = null, $product = null)
  296. {
  297. Varien_Profiler::start('CONFIGURABLE:'.__METHOD__);
  298. if (!$this->getProduct($product)->hasData($this->_usedProducts)) {
  299. if (is_null($requiredAttributeIds)
  300. and is_null($this->getProduct($product)->getData($this->_configurableAttributes))) {
  301. // If used products load before attributes, we will load attributes.
  302. $this->getConfigurableAttributes($product);
  303. // After attributes loading products loaded too.
  304. Varien_Profiler::stop('CONFIGURABLE:'.__METHOD__);
  305. return $this->getProduct($product)->getData($this->_usedProducts);
  306. }
  307. $usedProducts = array();
  308. $collection = $this->getUsedProductCollection($product)
  309. ->addAttributeToSelect('*')
  310. ->addFilterByRequiredOptions();
  311. if (is_array($requiredAttributeIds)) {
  312. foreach ($requiredAttributeIds as $attributeId) {
  313. $attribute = $this->getAttributeById($attributeId, $product);
  314. if (!is_null($attribute))
  315. $collection->addAttributeToFilter($attribute->getAttributeCode(), array('notnull'=>1));
  316. }
  317. }
  318. foreach ($collection as $item) {
  319. $usedProducts[] = $item;
  320. }
  321. $this->getProduct($product)->setData($this->_usedProducts, $usedProducts);
  322. }
  323. Varien_Profiler::stop('CONFIGURABLE:'.__METHOD__);
  324. return $this->getProduct($product)->getData($this->_usedProducts);
  325. }
  326. /**
  327. * Retrieve related products collection
  328. *
  329. * @param Mage_Catalog_Model_Product $product
  330. * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Type_Configurable_Product_Collection
  331. */
  332. public function getUsedProductCollection($product = null)
  333. {
  334. $collection = Mage::getResourceModel('catalog/product_type_configurable_product_collection')
  335. ->setFlag('require_stock_items', true)
  336. ->setFlag('product_children', true)
  337. ->setProductFilter($this->getProduct($product));
  338. if (!is_null($this->getStoreFilter($product))) {
  339. $collection->addStoreFilter($this->getStoreFilter($product));
  340. }
  341. return $collection;
  342. }
  343. /**
  344. * Before save process
  345. *
  346. * @param Mage_Catalog_Model_Product $product
  347. * @return Mage_Catalog_Model_Product_Type_Configurable
  348. */
  349. public function beforeSave($product = null)
  350. {
  351. parent::beforeSave($product);
  352. $this->getProduct($product)->canAffectOptions(false);
  353. if ($this->getProduct($product)->getCanSaveConfigurableAttributes()) {
  354. $this->getProduct($product)->canAffectOptions(true);
  355. if ($data = $this->getProduct($product)->getConfigurableAttributesData()) {
  356. if (!empty($data)) {
  357. foreach ($data as $attribute) {
  358. if (!empty($attribute['values'])) {
  359. $this->getProduct($product)->setTypeHasOptions(true);
  360. $this->getProduct($product)->setTypeHasRequiredOptions(true);
  361. break;
  362. }
  363. }
  364. }
  365. }
  366. }
  367. return $this;
  368. }
  369. /**
  370. * Save configurable product depended data
  371. *
  372. * @param Mage_Catalog_Model_Product $product
  373. * @return Mage_Catalog_Model_Product_Type_Configurable
  374. */
  375. public function save($product = null)
  376. {
  377. parent::save($product);
  378. /**
  379. * Save Attributes Information
  380. */
  381. if ($data = $this->getProduct($product)->getConfigurableAttributesData()) {
  382. foreach ($data as $attributeData) {
  383. $id = isset($attributeData['id']) ? $attributeData['id'] : null;
  384. Mage::getModel('catalog/product_type_configurable_attribute')
  385. ->setData($attributeData)
  386. ->setId($id)
  387. ->setStoreId($this->getProduct($product)->getStoreId())
  388. ->setProductId($this->getProduct($product)->getId())
  389. ->save();
  390. }
  391. }
  392. /**
  393. * Save product relations
  394. */
  395. $data = $this->getProduct($product)->getConfigurableProductsData();
  396. if (is_array($data)) {
  397. $productIds = array_keys($data);
  398. Mage::getResourceModel('catalog/product_type_configurable')
  399. ->saveProducts($this->getProduct($product), $productIds);
  400. }
  401. return $this;
  402. }
  403. /**
  404. * Check is product available for sale
  405. *
  406. * @return bool
  407. */
  408. public function isSalable($product = null)
  409. {
  410. $salable = parent::isSalable($product);
  411. if (!is_null($salable)) {
  412. return $salable;
  413. }
  414. $salable = false;
  415. foreach ($this->getUsedProducts(null, $product) as $child) {
  416. if ($child->isSalable()) {
  417. $salable = true;
  418. break;
  419. }
  420. }
  421. return $salable;
  422. }
  423. /**
  424. * Retrieve used product by attribute values
  425. * $attrbutesInfo = array(
  426. * $attributeId => $attributeValue
  427. * )
  428. *
  429. * @param array $attrbutesInfo
  430. * @param Mage_Catalog_Model_Product $product
  431. * @return Mage_Catalog_Model_Product|null
  432. */
  433. public function getProductByAttributes($attributesInfo, $product = null)
  434. {
  435. if (is_array($attributesInfo) && !empty($attributesInfo)) {
  436. foreach ($this->getUsedProducts(null, $product) as $productObject) {
  437. $checkRes = true;
  438. foreach ($attributesInfo as $attributeId => $attributeValue) {
  439. $code = $this->getAttributeById($attributeId, $product)->getAttributeCode();
  440. if ($productObject->getData($code) != $attributeValue) {
  441. $checkRes = false;
  442. }
  443. }
  444. if ($checkRes) {
  445. return $productObject;
  446. }
  447. }
  448. }
  449. return null;
  450. }
  451. /**
  452. * Retrieve Selected Attributes info
  453. *
  454. * @param Mage_Catalog_Model_Product $product
  455. * @return array
  456. */
  457. public function getSelectedAttributesInfo($product = null)
  458. {
  459. $attributes = array();
  460. Varien_Profiler::start('CONFIGURABLE:'.__METHOD__);
  461. if ($attributesOption = $this->getProduct($product)->getCustomOption('attributes')) {
  462. $data = unserialize($attributesOption->getValue());
  463. $this->getUsedProductAttributeIds($product);
  464. $usedAttributes = $this->getProduct($product)->getData($this->_usedAttributes);
  465. foreach ($data as $attributeId => $attributeValue) {
  466. if (isset($usedAttributes[$attributeId])) {
  467. $attribute = $usedAttributes[$attributeId];
  468. $label = $attribute->getLabel();
  469. $value = $attribute->getProductAttribute();
  470. if ($value->getSourceModel()) {
  471. $value = $value->getSource()->getOptionText($attributeValue);
  472. }
  473. else {
  474. $value = '';
  475. }
  476. $attributes[] = array('label'=>$label, 'value'=>$value);
  477. }
  478. }
  479. }
  480. Varien_Profiler::stop('CONFIGURABLE:'.__METHOD__);
  481. return $attributes;
  482. }
  483. /**
  484. * Prepare product and its configuration to be added to some products list.
  485. * Perform standard preparation process and then add Configurable specific options.
  486. *
  487. * @param Varien_Object $buyRequest
  488. * @param Mage_Catalog_Model_Product $product
  489. * @param string $processMode
  490. * @return array|string
  491. */
  492. protected function _prepareProduct(Varien_Object $buyRequest, $product, $processMode)
  493. {
  494. $attributes = $buyRequest->getSuperAttribute();
  495. if ($attributes || !$this->_isStrictProcessMode($processMode)) {
  496. if (!$this->_isStrictProcessMode($processMode)) {
  497. if (is_array($attributes)) {
  498. foreach ($attributes as $key => $val) {
  499. if (empty($val)) {
  500. unset($attributes[$key]);
  501. }
  502. }
  503. } else {
  504. $attributes = array();
  505. }
  506. }
  507. $result = parent::_prepareProduct($buyRequest, $product, $processMode);
  508. if (is_array($result)) {
  509. $product = $this->getProduct($product);
  510. /**
  511. * $attributes = array($attributeId=>$attributeValue)
  512. */
  513. $subProduct = $this->getProductByAttributes($attributes, $product);
  514. if ($subProduct) {
  515. $product->addCustomOption('attributes', serialize($attributes));
  516. $product->addCustomOption('product_qty_'.$subProduct->getId(), 1, $subProduct);
  517. $product->addCustomOption('simple_product', $subProduct->getId(), $subProduct);
  518. $_result = $subProduct->getTypeInstance(true)->_prepareProduct($buyRequest, $subProduct, $processMode);
  519. if (is_string($_result) && !is_array($_result)) {
  520. return $_result;
  521. }
  522. if (!isset($_result[0])) {
  523. return Mage::helper('checkout')->__('Cannot add the item to shopping cart');
  524. }
  525. /**
  526. * Adding parent product custom options to child product
  527. * to be sure that it will be unique as its parent
  528. */
  529. if ($optionIds = $product->getCustomOption('option_ids')) {
  530. $optionIds = explode(',', $optionIds->getValue());
  531. foreach ($optionIds as $optionId) {
  532. if ($option = $product->getCustomOption('option_' . $optionId)) {
  533. $_result[0]->addCustomOption('option_' . $optionId, $option->getValue());
  534. }
  535. }
  536. }
  537. $_result[0]->setParentProductId($product->getId())
  538. // add custom option to simple product for protection of process when we add simple product separately
  539. ->addCustomOption('parent_product_id', $product->getId());
  540. if ($this->_isStrictProcessMode($processMode)) {
  541. $_result[0]->setCartQty(1);
  542. }
  543. $result[] = $_result[0];
  544. return $result;
  545. } else if (!$this->_isStrictProcessMode($processMode)) {
  546. return $result;
  547. }
  548. }
  549. }
  550. return $this->getSpecifyOptionMessage();
  551. }
  552. /**
  553. * Check if product can be bought
  554. *
  555. * @param Mage_Catalog_Model_Product $product
  556. * @return Mage_Catalog_Model_Product_Type_Abstract
  557. * @throws Mage_Core_Exception
  558. */
  559. public function checkProductBuyState($product = null)
  560. {
  561. parent::checkProductBuyState($product);
  562. $product = $this->getProduct($product);
  563. $option = $product->getCustomOption('info_buyRequest');
  564. if ($option instanceof Mage_Sales_Model_Quote_Item_Option) {
  565. $buyRequest = new Varien_Object(unserialize($option->getValue()));
  566. $attributes = $buyRequest->getSuperAttribute();
  567. if (is_array($attributes)) {
  568. foreach ($attributes as $key => $val) {
  569. if (empty($val)) {
  570. unset($attributes[$key]);
  571. }
  572. }
  573. }
  574. if (empty($attributes)) {
  575. Mage::throwException($this->getSpecifyOptionMessage());
  576. }
  577. }
  578. return $this;
  579. }
  580. /**
  581. * Retrieve message for specify option(s)
  582. *
  583. * @return string
  584. */
  585. public function getSpecifyOptionMessage()
  586. {
  587. return Mage::helper('catalog')->__('Please specify the product\'s option(s).');
  588. }
  589. /**
  590. * Prepare additional options/information for order item which will be
  591. * created from this product
  592. *
  593. * @param Mage_Catalog_Model_Product $product
  594. * @return array
  595. */
  596. public function getOrderOptions($product = null)
  597. {
  598. $options = parent::getOrderOptions($product);
  599. $options['attributes_info'] = $this->getSelectedAttributesInfo($product);
  600. if ($simpleOption = $this->getProduct($product)->getCustomOption('simple_product')) {
  601. $options['simple_name'] = $simpleOption->getProduct($product)->getName();
  602. $options['simple_sku'] = $simpleOption->getProduct($product)->getSku();
  603. }
  604. $options['product_calculations'] = self::CALCULATE_PARENT;
  605. $options['shipment_type'] = self::SHIPMENT_TOGETHER;
  606. return $options;
  607. }
  608. /**
  609. * Check is virtual product
  610. *
  611. * @return bool
  612. */
  613. public function isVirtual($product = null)
  614. {
  615. if ($productOption = $this->getProduct($product)->getCustomOption('simple_product')) {
  616. if ($optionProduct = $productOption->getProduct()) {
  617. /* @var $optionProduct Mage_Catalog_Model_Product */
  618. return $optionProduct->isVirtual();
  619. }
  620. }
  621. return parent::isVirtual($product);
  622. }
  623. /**
  624. * Return true if product has options
  625. *
  626. * @param Mage_Catalog_Model_Product $product
  627. * @return bool
  628. */
  629. public function hasOptions($product = null)
  630. {
  631. if ($this->getProduct($product)->getOptions()) {
  632. return true;
  633. }
  634. $attributes = $this->getConfigurableAttributes($product);
  635. if (count($attributes)) {
  636. foreach ($attributes as $attribute) {
  637. /** @var Mage_Catalog_Model_Product_Type_Configurable_Attribute $attribute */
  638. if ($attribute->getData('prices')) {
  639. return true;
  640. }
  641. }
  642. }
  643. return false;
  644. }
  645. /**
  646. * Return product weight based on simple product
  647. * weight or configurable product weight
  648. *
  649. * @param Mage_Catalog_Model_Product $product
  650. * @return decimal
  651. */
  652. public function getWeight($product = null)
  653. {
  654. if ($this->getProduct($product)->hasCustomOptions() && ($simpleProductOption = $this->getProduct($product)->getCustomOption('simple_product'))) {
  655. $simpleProduct = $simpleProductOption->getProduct($product);
  656. if ($simpleProduct) {
  657. return $simpleProduct->getWeight();
  658. }
  659. }
  660. return $this->getProduct($product)->getData('weight');
  661. }
  662. /**
  663. * Implementation of product specify logic of which product needs to be assigned to option.
  664. * For example if product which was added to option already removed from catalog.
  665. *
  666. * @param Mage_Catalog_Model_Product $optionProduct
  667. * @param Mage_Sales_Model_Quote_Item_Option $option
  668. * @param Mage_Catalog_Model_Product $product
  669. * @return Mage_Catalog_Model_Product_Type_Abstract
  670. */
  671. public function assignProductToOption($optionProduct, $option, $product = null)
  672. {
  673. if ($optionProduct) {
  674. $option->setProduct($optionProduct);
  675. } else {
  676. $option->getItem()->setHasError('error');
  677. $option->getItem()->addMessage(Mage::helper('catalog')->__('Selected configuration is not available.', $this->getProduct($product)->getName()));
  678. }
  679. return $this;
  680. }
  681. /**
  682. * Retrieve products divided into groups required to purchase
  683. * At least one product in each group has to be purchased
  684. *
  685. * @param Mage_Catalog_Model_Product $product
  686. * @return array
  687. */
  688. public function getProductsToPurchaseByReqGroups($product = null)
  689. {
  690. $product = $this->getProduct($product);
  691. return array($this->getUsedProducts(null, $product));
  692. }
  693. /**
  694. * Get sku of product
  695. *
  696. * @param Mage_Catalog_Model_Product $product
  697. * @return string
  698. */
  699. public function getSku($product = null)
  700. {
  701. $sku = $this->getProduct($product)->getData('sku');
  702. if ($simpleOption = $this->getProduct($product)->getCustomOption('simple_product')) {
  703. $simple_sku = $simpleOption->getProduct($product)->getSku();
  704. $sku = parent::getOptionSku($product, $simple_sku);
  705. } else {
  706. $sku = parent::getSku($product);
  707. }
  708. return $sku;
  709. }
  710. /**
  711. * Prepare selected options for configurable product
  712. *
  713. * @param Mage_Catalog_Model_Product $product
  714. * @param Varien_Object $buyRequest
  715. * @return array
  716. */
  717. public function processBuyRequest($product, $buyRequest)
  718. {
  719. $superAttribute = $buyRequest->getSuperAttribute();
  720. $superAttribute = (is_array($superAttribute)) ? array_filter($superAttribute, 'intval') : array();
  721. $options = array('super_attribute' => $superAttribute);
  722. return $options;
  723. }
  724. }