Advertisement
rbncha

Mage_Catalog_Model_Product_Type_Configurable

Aug 26th, 2017
634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 31.14 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Configurable product type implementation
  5.  *
  6.  * This type builds in product attributes and existing simple products
  7.  *
  8.  * @category   Mage
  9.  * @package    Mage_Catalog
  10.  * @author     Magento Core Team <core@magentocommerce.com>
  11.  */
  12. class Mage_Catalog_Model_Product_Type_Configurable extends Mage_Catalog_Model_Product_Type_Abstract
  13. {
  14.     const TYPE_CODE = 'configurable';
  15.  
  16.     /**
  17.      * Cache key for Used Product Attribute Ids
  18.      *
  19.      * @var string
  20.      */
  21.     protected $_usedProductAttributeIds = '_cache_instance_used_product_attribute_ids';
  22.  
  23.     /**
  24.      * Cache key for Used Product Attributes
  25.      *
  26.      * @var string
  27.      */
  28.     protected $_usedProductAttributes   = '_cache_instance_used_product_attributes';
  29.  
  30.     /**
  31.      * Cache key for Used Attributes
  32.      *
  33.      * @var string
  34.      */
  35.     protected $_usedAttributes          = '_cache_instance_used_attributes';
  36.  
  37.     /**
  38.      * Cache key for configurable attributes
  39.      *
  40.      * @var string
  41.      */
  42.     protected $_configurableAttributes  = '_cache_instance_configurable_attributes';
  43.  
  44.     /**
  45.      * Cache key for Used product ids
  46.      *
  47.      * @var string
  48.      */
  49.     protected $_usedProductIds          = '_cache_instance_product_ids';
  50.  
  51.     /**
  52.      * Cache key for used products
  53.      *
  54.      * @var string
  55.      */
  56.     protected $_usedProducts            = '_cache_instance_products';
  57.  
  58.     /**
  59.      * Product is composite
  60.      *
  61.      * @var bool
  62.      */
  63.     protected $_isComposite             = true;
  64.  
  65.     /**
  66.      * Product is configurable
  67.      *
  68.      * @var bool
  69.      */
  70.     protected $_canConfigure            = true;
  71.  
  72.     /**
  73.      * Product attributes to include on the children of configurable products
  74.      *
  75.      * @var string
  76.      */
  77.     const XML_PATH_PRODUCT_CONFIGURABLE_CHILD_ATTRIBUTES = 'frontend/product/configurable/child/attributes';
  78.  
  79.     /**
  80.      * Return relation info about used products
  81.      *
  82.      * @return Varien_Object Object with information data
  83.      */
  84.     public function getRelationInfo()
  85.     {
  86.         $info = new Varien_Object();
  87.         $info->setTable('catalog/product_super_link')
  88.             ->setParentFieldName('parent_id')
  89.             ->setChildFieldName('product_id');
  90.         return $info;
  91.     }
  92.  
  93.     /**
  94.      * Retrieve Required children ids
  95.      * Return grouped array, ex array(
  96.      *   group => array(ids)
  97.      * )
  98.      *
  99.      * @param  int $parentId
  100.      * @param  bool $required
  101.      * @return array
  102.      */
  103.     public function getChildrenIds($parentId, $required = true)
  104.     {
  105.         return Mage::getResourceSingleton('catalog/product_type_configurable')
  106.             ->getChildrenIds($parentId, $required);
  107.     }
  108.  
  109.     /**
  110.      * Retrieve parent ids array by required child
  111.      *
  112.      * @param  int|array $childId
  113.      * @return array
  114.      */
  115.     public function getParentIdsByChild($childId)
  116.     {
  117.         return Mage::getResourceSingleton('catalog/product_type_configurable')
  118.             ->getParentIdsByChild($childId);
  119.     }
  120.  
  121.     /**
  122.      * Retrieve product type attributes
  123.      *
  124.      * @param  Mage_Catalog_Model_Product $product
  125.      * @return array
  126.      */
  127.     public function getEditableAttributes($product = null)
  128.     {
  129.         if (is_null($this->_editableAttributes)) {
  130.             $this->_editableAttributes = parent::getEditableAttributes($product);
  131.             foreach ($this->_editableAttributes as $index => $attribute) {
  132.                 if ($this->getUsedProductAttributeIds($product)
  133.                     && in_array($attribute->getAttributeId(), $this->getUsedProductAttributeIds($product))) {
  134.                     unset($this->_editableAttributes[$index]);
  135.                 }
  136.             }
  137.         }
  138.         return $this->_editableAttributes;
  139.     }
  140.  
  141.     /**
  142.      * Checkin attribute availability for create superproduct
  143.      *
  144.      * @param   Mage_Eav_Model_Entity_Attribute $attribute
  145.      * @return  bool
  146.      */
  147.     public function canUseAttribute(Mage_Eav_Model_Entity_Attribute $attribute)
  148.     {
  149.         $allow = $attribute->getIsGlobal() == Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL
  150.             && $attribute->getIsVisible()
  151.             && $attribute->getIsConfigurable()
  152.             && $attribute->usesSource()
  153.             && $attribute->getIsUserDefined();
  154.  
  155.         return $allow;
  156.     }
  157.  
  158.     /**
  159.      * Declare attribute identifiers used for assign subproducts
  160.      *
  161.      * @param   array $ids
  162.      * @param   Mage_Catalog_Model_Product $product
  163.      * @return  Mage_Catalog_Model_Product_Type_Configurable
  164.      */
  165.     public function setUsedProductAttributeIds($ids, $product = null)
  166.     {
  167.         $usedProductAttributes  = array();
  168.         $configurableAttributes = array();
  169.  
  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.  
  179.         return $this;
  180.     }
  181.  
  182.     /**
  183.      * Retrieve identifiers of used product attributes
  184.      *
  185.      * @param  Mage_Catalog_Model_Product $product
  186.      * @return array
  187.      */
  188.     public function getUsedProductAttributeIds($product = null)
  189.     {
  190.         if (!$this->getProduct($product)->hasData($this->_usedProductAttributeIds)) {
  191.             $usedProductAttributeIds = array();
  192.             foreach ($this->getUsedProductAttributes($product) as $attribute) {
  193.                 $usedProductAttributeIds[] = $attribute->getId();
  194.             }
  195.             $this->getProduct($product)->setData($this->_usedProductAttributeIds, $usedProductAttributeIds);
  196.         }
  197.         return $this->getProduct($product)->getData($this->_usedProductAttributeIds);
  198.     }
  199.  
  200.     /**
  201.      * Retrieve used product attributes
  202.      *
  203.      * @param  Mage_Catalog_Model_Product $product
  204.      * @return array
  205.      */
  206.     public function getUsedProductAttributes($product = null)
  207.     {
  208.         if (!$this->getProduct($product)->hasData($this->_usedProductAttributes)) {
  209.             $usedProductAttributes = array();
  210.             $usedAttributes        = array();
  211.             foreach ($this->getConfigurableAttributes($product) as $attribute) {
  212.                 if (!is_null($attribute->getProductAttribute())) {
  213.                     $id = $attribute->getProductAttribute()->getId();
  214.                     $usedProductAttributes[$id] = $attribute->getProductAttribute();
  215.                     $usedAttributes[$id]        = $attribute;
  216.                 }
  217.             }
  218.             $this->getProduct($product)->setData($this->_usedAttributes, $usedAttributes);
  219.             $this->getProduct($product)->setData($this->_usedProductAttributes, $usedProductAttributes);
  220.         }
  221.         return $this->getProduct($product)->getData($this->_usedProductAttributes);
  222.     }
  223.  
  224.     /**
  225.      * Retrieve configurable attributes data
  226.      *
  227.      * @param  Mage_Catalog_Model_Product $product
  228.      * @return array
  229.      */
  230.     public function getConfigurableAttributes($product = null)
  231.     {
  232.         Varien_Profiler::start('CONFIGURABLE:'.__METHOD__);
  233.         if (!$this->getProduct($product)->hasData($this->_configurableAttributes)) {
  234.             $configurableAttributes = $this->getConfigurableAttributeCollection($product)
  235.                 ->orderByPosition()
  236.                 ->load();
  237.             $this->getProduct($product)->setData($this->_configurableAttributes, $configurableAttributes);
  238.         }
  239.         Varien_Profiler::stop('CONFIGURABLE:'.__METHOD__);
  240.         return $this->getProduct($product)->getData($this->_configurableAttributes);
  241.     }
  242.  
  243.     /**
  244.      * Retrieve Configurable Attributes as array
  245.      *
  246.      * @param  Mage_Catalog_Model_Product $product
  247.      * @return array
  248.      */
  249.     public function getConfigurableAttributesAsArray($product = null)
  250.     {
  251.         $res = array();
  252.         foreach ($this->getConfigurableAttributes($product) as $attribute) {
  253.             $res[] = array(
  254.                 'id'             => $attribute->getId(),
  255.                 'label'          => $attribute->getLabel(),
  256.                 'use_default'    => $attribute->getUseDefault(),
  257.                 'position'       => $attribute->getPosition(),
  258.                 'values'         => $attribute->getPrices() ? $attribute->getPrices() : array(),
  259.                 'attribute_id'   => $attribute->getProductAttribute()->getId(),
  260.                 'attribute_code' => $attribute->getProductAttribute()->getAttributeCode(),
  261.                 'frontend_label' => $attribute->getProductAttribute()->getFrontend()->getLabel(),
  262.                 'store_label'    => $attribute->getProductAttribute()->getStoreLabel(),
  263.             );
  264.         }
  265.         return $res;
  266.     }
  267.  
  268.     /**
  269.      * Retrieve configurable attribute collection
  270.      *
  271.      * @param Mage_Catalog_Model_Product $product
  272.      * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Type_Configurable_Attribute_Collection
  273.      */
  274.     public function getConfigurableAttributeCollection($product = null)
  275.     {
  276.         return Mage::getResourceModel('catalog/product_type_configurable_attribute_collection')
  277.             ->setProductFilter($this->getProduct($product));
  278.     }
  279.  
  280.  
  281.     /**
  282.      * Retrieve subproducts identifiers
  283.      *
  284.      * @param  Mage_Catalog_Model_Product $product
  285.      * @return array
  286.      */
  287.     public function getUsedProductIds($product = null)
  288.     {
  289.         if (!$this->getProduct($product)->hasData($this->_usedProductIds)) {
  290.             $usedProductIds = array();
  291.             foreach ($this->getUsedProducts(null, $product) as $product) {
  292.                 $usedProductIds[] = $product->getId();
  293.             }
  294.             $this->getProduct($product)->setData($this->_usedProductIds, $usedProductIds);
  295.         }
  296.         return $this->getProduct($product)->getData($this->_usedProductIds);
  297.     }
  298.  
  299.     /**
  300.      * Retrieve array of "subproducts"
  301.      *
  302.      * @param  array
  303.      * @param  Mage_Catalog_Model_Product $product
  304.      * @return array
  305.      */
  306.     public function getUsedProducts($requiredAttributeIds = null, $product = null)
  307.     {
  308.         Varien_Profiler::start('CONFIGURABLE:'.__METHOD__);
  309.         if (!$this->getProduct($product)->hasData($this->_usedProducts)) {
  310.             if (is_null($requiredAttributeIds)
  311.                 and is_null($this->getProduct($product)->getData($this->_configurableAttributes))) {
  312.                 // If used products load before attributes, we will load attributes.
  313.                 $this->getConfigurableAttributes($product);
  314.                 // After attributes loading products loaded too.
  315.                 Varien_Profiler::stop('CONFIGURABLE:'.__METHOD__);
  316.                 return $this->getProduct($product)->getData($this->_usedProducts);
  317.             }
  318.  
  319.             $usedProducts = array();
  320.             $collection = $this->getUsedProductCollection($product)
  321.                 ->addFilterByRequiredOptions();
  322.  
  323.             // Provides a mechanism for attaching additional attributes to the children of configurable products
  324.             // Will primarily have affect on the configurable product view page
  325.             $childAttributes = Mage::getConfig()->getNode(self::XML_PATH_PRODUCT_CONFIGURABLE_CHILD_ATTRIBUTES);
  326.  
  327.             if ($childAttributes) {
  328.                 $childAttributes = $childAttributes->asArray();
  329.                 $childAttributes = array_keys($childAttributes);
  330.  
  331.                 $collection->addAttributeToSelect($childAttributes);
  332.             }
  333.  
  334.             if (is_array($requiredAttributeIds)) {
  335.                 foreach ($requiredAttributeIds as $attributeId) {
  336.                     $attribute = $this->getAttributeById($attributeId, $product);
  337.                     if (!is_null($attribute))
  338.                         $collection->addAttributeToFilter($attribute->getAttributeCode(), array('notnull'=>1));
  339.                 }
  340.             }
  341.  
  342.             foreach ($collection as $item) {
  343.                 $usedProducts[] = $item;
  344.             }
  345.  
  346.             $this->getProduct($product)->setData($this->_usedProducts, $usedProducts);
  347.         }
  348.         Varien_Profiler::stop('CONFIGURABLE:'.__METHOD__);
  349.         return $this->getProduct($product)->getData($this->_usedProducts);
  350.     }
  351.  
  352.     /**
  353.      * Retrieve related products collection
  354.      *
  355.      * @param  Mage_Catalog_Model_Product $product
  356.      * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Type_Configurable_Product_Collection
  357.      */
  358.     public function getUsedProductCollection($product = null)
  359.     {
  360.         $collection = Mage::getResourceModel('catalog/product_type_configurable_product_collection')
  361.             ->setFlag('require_stock_items', true)
  362.             ->setFlag('product_children', true)
  363.             ->setProductFilter($this->getProduct($product));
  364.         if (!is_null($this->getStoreFilter($product))) {
  365.             $collection->addStoreFilter($this->getStoreFilter($product));
  366.         }
  367.  
  368.         return $collection;
  369.     }
  370.  
  371.     /**
  372.      * Before save process
  373.      *
  374.      * @param  Mage_Catalog_Model_Product $product
  375.      * @return Mage_Catalog_Model_Product_Type_Configurable
  376.      */
  377.     public function beforeSave($product = null)
  378.     {
  379.         parent::beforeSave($product);
  380.  
  381.         $this->getProduct($product)->canAffectOptions(false);
  382.  
  383.         if ($this->getProduct($product)->getCanSaveConfigurableAttributes()) {
  384.             $this->getProduct($product)->canAffectOptions(true);
  385.             $data = $this->getProduct($product)->getConfigurableAttributesData();
  386.             if (!empty($data)) {
  387.                 foreach ($data as $attribute) {
  388.                     if (!empty($attribute['values'])) {
  389.                         $this->getProduct($product)->setTypeHasOptions(true);
  390.                         $this->getProduct($product)->setTypeHasRequiredOptions(true);
  391.                         break;
  392.                     }
  393.                 }
  394.             }
  395.         }
  396.         foreach ($this->getConfigurableAttributes($product) as $attribute) {
  397.             $this->getProduct($product)->setData($attribute->getProductAttribute()->getAttributeCode(), null);
  398.         }
  399.  
  400.         return $this;
  401.     }
  402.  
  403.     /**
  404.      * Save configurable product depended data
  405.      *
  406.      * @param  Mage_Catalog_Model_Product $product
  407.      * @return Mage_Catalog_Model_Product_Type_Configurable
  408.      */
  409.     public function save($product = null)
  410.     {
  411.         parent::save($product);
  412.         /**
  413.          * Save Attributes Information
  414.          */
  415.         if ($data = $this->getProduct($product)->getConfigurableAttributesData()) {
  416.             foreach ($data as $attributeData) {
  417.                 $id = isset($attributeData['id']) ? $attributeData['id'] : null;
  418.                 Mage::getModel('catalog/product_type_configurable_attribute')
  419.                    ->setData($attributeData)
  420.                    ->setId($id)
  421.                    ->setStoreId($this->getProduct($product)->getStoreId())
  422.                    ->setProductId($this->getProduct($product)->getId())
  423.                    ->save();
  424.             }
  425.         }
  426.  
  427.         /**
  428.          * Save product relations
  429.          */
  430.         $data = $this->getProduct($product)->getConfigurableProductsData();
  431.         if (is_array($data)) {
  432.             $productIds = array_keys($data);
  433.             Mage::getResourceModel('catalog/product_type_configurable')
  434.                 ->saveProducts($this->getProduct($product), $productIds);
  435.         }
  436.         return $this;
  437.     }
  438.  
  439.     /**
  440.      * Check is product available for sale
  441.      *
  442.      * @param Mage_Catalog_Model_Product $product
  443.      * @return bool
  444.      */
  445.     public function isSalable($product = null)
  446.     {
  447.         $salable = parent::isSalable($product);
  448.  
  449.         if ($salable !== false) {
  450.             $salable = false;
  451.             if (!is_null($product)) {
  452.                 $this->setStoreFilter($product->getStoreId(), $product);
  453.             }
  454.             foreach ($this->getUsedProducts(null, $product) as $child) {
  455.                 if ($child->isSalable()) {
  456.                     $salable = true;
  457.                     break;
  458.                 }
  459.             }
  460.         }
  461.  
  462.         return $salable;
  463.     }
  464.  
  465.     /**
  466.      * Check whether the product is available for sale
  467.      * is alias to isSalable for compatibility
  468.      *
  469.      * @param Mage_Catalog_Model_Product $product
  470.      * @return bool
  471.      */
  472.     public function getIsSalable($product = null)
  473.     {
  474.         return $this->isSalable($product);
  475.     }
  476.  
  477.     /**
  478.      * Retrieve used product by attribute values
  479.      *  $attrbutesInfo = array(
  480.      *      $attributeId => $attributeValue
  481.      *  )
  482.      *
  483.      * @param  array $attributesInfo
  484.      * @param  Mage_Catalog_Model_Product $product
  485.      * @return Mage_Catalog_Model_Product|null
  486.      */
  487.     public function getProductByAttributes($attributesInfo, $product = null)
  488.     {
  489.         if (is_array($attributesInfo) && !empty($attributesInfo)) {
  490.             $productCollection = $this->getUsedProductCollection($product)->addAttributeToSelect('name');
  491.            
  492.             foreach ($attributesInfo as $attributeId => $attributeValue) {
  493.                 $productCollection->addAttributeToFilter($attributeId, $attributeValue);
  494.             }
  495.  
  496.             $productObject = $productCollection->getFirstItem();
  497.             if ($productObject->getId()) {
  498.  
  499.  
  500.                 return $productObject;
  501.             }
  502.  
  503.             Mage::log($attributesInfo, null, 'debug-product-options.txt');
  504.  
  505.             foreach ($this->getUsedProducts(null, $product) as $productObject) {
  506.                 $checkRes = true;
  507.                 foreach ($attributesInfo as $attributeId => $attributeValue) {
  508.                     $code = $this->getAttributeById($attributeId, $product)->getAttributeCode();
  509.  
  510.                     if ($productObject->getData($code) != $attributeValue) {
  511.                         $checkRes = false;
  512.                     }
  513.                 }
  514.  
  515.                
  516.  
  517.                 if ($checkRes) {
  518.                     return $productObject;
  519.                 }
  520.             }
  521.         }
  522.         return null;
  523.     }
  524.  
  525.     /**
  526.      * Retrieve Selected Attributes info
  527.      *
  528.      * @param  Mage_Catalog_Model_Product $product
  529.      * @return array
  530.      */
  531.     public function getSelectedAttributesInfo($product = null)
  532.     {
  533.         $attributes = array();
  534.         Varien_Profiler::start('CONFIGURABLE:'.__METHOD__);
  535.         if ($attributesOption = $this->getProduct($product)->getCustomOption('attributes')) {
  536.             $data = unserialize($attributesOption->getValue());
  537.             $this->getUsedProductAttributeIds($product);
  538.  
  539.             $usedAttributes = $this->getProduct($product)->getData($this->_usedAttributes);
  540.  
  541.             foreach ($data as $attributeId => $attributeValue) {
  542.                 if (isset($usedAttributes[$attributeId])) {
  543.                     $attribute = $usedAttributes[$attributeId];
  544.                     $label = $attribute->getLabel();
  545.                     $value = $attribute->getProductAttribute();
  546.                     if ($value->getSourceModel()) {
  547.                         $value = $value->getSource()->getOptionText($attributeValue);
  548.                     }
  549.                     else {
  550.                         $value = '';
  551.                     }
  552.  
  553.                     $attributes[] = array('label'=>$label, 'value'=>$value);
  554.                 }
  555.             }
  556.         }
  557.         Varien_Profiler::stop('CONFIGURABLE:'.__METHOD__);
  558.         return $attributes;
  559.     }
  560.  
  561.     /**
  562.      * Prepare product and its configuration to be added to some products list.
  563.      * Perform standard preparation process and then add Configurable specific options.
  564.      *
  565.      * @param Varien_Object $buyRequest
  566.      * @param Mage_Catalog_Model_Product $product
  567.      * @param string $processMode
  568.      * @return array|string
  569.      */
  570.     protected function _prepareProduct(Varien_Object $buyRequest, $product, $processMode)
  571.     {
  572.         $attributes = $buyRequest->getSuperAttribute();
  573.         if ($attributes || !$this->_isStrictProcessMode($processMode)) {
  574.             if (!$this->_isStrictProcessMode($processMode)) {
  575.                 if (is_array($attributes)) {
  576.                     foreach ($attributes as $key => $val) {
  577.                         if (empty($val)) {
  578.                             unset($attributes[$key]);
  579.                         }
  580.                     }
  581.                 } else {
  582.                     $attributes = array();
  583.                 }
  584.             }
  585.  
  586.             $result = parent::_prepareProduct($buyRequest, $product, $processMode);
  587.             if (is_array($result)) {
  588.                 $product = $this->getProduct($product);
  589.                 /**
  590.                  * $attributes = array($attributeId=>$attributeValue)
  591.                  */
  592.                 $subProduct = true;
  593.                 if ($this->_isStrictProcessMode($processMode)) {
  594.                     foreach($this->getConfigurableAttributes($product) as $attributeItem){
  595.                         /* @var $attributeItem Varien_Object */
  596.                         $attrId = $attributeItem->getData('attribute_id');
  597.                         if(!isset($attributes[$attrId]) || empty($attributes[$attrId])) {
  598.                             $subProduct = null;
  599.                             break;
  600.                         }
  601.                     }
  602.                 }
  603.                 if( $subProduct ) {
  604.                     $subProduct = $this->getProductByAttributes($attributes, $product);
  605.                 }
  606.  
  607.                 Mage::log(print_r($attributes,true),null,'last.txt');
  608.  
  609.                 if ($subProduct) {
  610.                     $product->addCustomOption('attributes', serialize($attributes));
  611.                     $product->addCustomOption('product_qty_'.$subProduct->getId(), 1, $subProduct);
  612.                     $product->addCustomOption('simple_product', $subProduct->getId(), $subProduct);
  613.  
  614.                     $_result = $subProduct->getTypeInstance(true)->_prepareProduct(
  615.                         $buyRequest,
  616.                         $subProduct,
  617.                         $processMode
  618.                     );
  619.                     if (is_string($_result) && !is_array($_result)) {
  620.                         return $_result;
  621.                     }
  622.  
  623.                     if (!isset($_result[0])) {
  624.                         return Mage::helper('checkout')->__('Cannot add the item to shopping cart');
  625.                     }
  626.  
  627.                     /**
  628.                      * Adding parent product custom options to child product
  629.                      * to be sure that it will be unique as its parent
  630.                      */
  631.                     if ($optionIds = $product->getCustomOption('option_ids')) {
  632.                         $optionIds = explode(',', $optionIds->getValue());
  633.                         foreach ($optionIds as $optionId) {
  634.                             if ($option = $product->getCustomOption('option_' . $optionId)) {
  635.                                 $_result[0]->addCustomOption('option_' . $optionId, $option->getValue());
  636.                             }
  637.                         }
  638.                     }
  639.  
  640.                     $_result[0]->setParentProductId($product->getId())
  641.                         // add custom option to simple product for protection of process
  642.                         //when we add simple product separately
  643.                         ->addCustomOption('parent_product_id', $product->getId());
  644.                     if ($this->_isStrictProcessMode($processMode)) {
  645.                         $_result[0]->setCartQty(1);
  646.                     }
  647.                     $result[] = $_result[0];
  648.                     return $result;
  649.                 } else if (!$this->_isStrictProcessMode($processMode)) {
  650.                     return $result;
  651.                 }
  652.             }
  653.         }
  654.  
  655.         return $this->getSpecifyOptionMessage();
  656.     }
  657.  
  658.     /**
  659.      * Check if product can be bought
  660.      *
  661.      * @param  Mage_Catalog_Model_Product $product
  662.      * @return Mage_Catalog_Model_Product_Type_Configurable
  663.      * @throws Mage_Core_Exception
  664.      */
  665.     public function checkProductBuyState($product = null)
  666.     {
  667.         parent::checkProductBuyState($product);
  668.         $product = $this->getProduct($product);
  669.         $option = $product->getCustomOption('info_buyRequest');
  670.         if ($option instanceof Mage_Sales_Model_Quote_Item_Option) {
  671.             $buyRequest = new Varien_Object(unserialize($option->getValue()));
  672.             $attributes = $buyRequest->getSuperAttribute();
  673.             if (is_array($attributes)) {
  674.                 foreach ($attributes as $key => $val) {
  675.                     if (empty($val)) {
  676.                         unset($attributes[$key]);
  677.                     }
  678.                 }
  679.             }
  680.             if (empty($attributes)) {
  681.                 Mage::throwException($this->getSpecifyOptionMessage());
  682.             }
  683.         }
  684.         return $this;
  685.     }
  686.  
  687.     /**
  688.      * Retrieve message for specify option(s)
  689.      *
  690.      * @return string
  691.      */
  692.     public function getSpecifyOptionMessage()
  693.     {
  694.         //Mage::log('Please specify the product\'s option(s).', null, 'last.txt');
  695.         return Mage::helper('catalog')->__('Please specify the product\'s option(s).');
  696.     }
  697.  
  698.     /**
  699.      * Prepare additional options/information for order item which will be
  700.      * created from this product
  701.      *
  702.      * @param  Mage_Catalog_Model_Product $product
  703.      * @return array
  704.      */
  705.     public function getOrderOptions($product = null)
  706.     {
  707.         $options = parent::getOrderOptions($product);
  708.         $options['attributes_info'] = $this->getSelectedAttributesInfo($product);
  709.         if ($simpleOption = $this->getProduct($product)->getCustomOption('simple_product')) {
  710.             $options['simple_name'] = $simpleOption->getProduct($product)->getName();
  711.             $options['simple_sku']  = $simpleOption->getProduct($product)->getSku();
  712.         }
  713.  
  714.         $options['product_calculations'] = self::CALCULATE_PARENT;
  715.         $options['shipment_type'] = self::SHIPMENT_TOGETHER;
  716.  
  717.         return $options;
  718.     }
  719.  
  720.     /**
  721.      * Check is virtual product
  722.      *
  723.      * @param Mage_Catalog_Model_Product $product
  724.      * @return bool
  725.      */
  726.     public function isVirtual($product = null)
  727.     {
  728.         if ($productOption = $this->getProduct($product)->getCustomOption('simple_product')) {
  729.             if ($optionProduct = $productOption->getProduct()) {
  730.                 /* @var $optionProduct Mage_Catalog_Model_Product */
  731.                 return $optionProduct->isVirtual();
  732.             }
  733.         }
  734.         return parent::isVirtual($product);
  735.     }
  736.  
  737.     /**
  738.      * Return true if product has options
  739.      *
  740.      * @param  Mage_Catalog_Model_Product $product
  741.      * @return bool
  742.      */
  743.     public function hasOptions($product = null)
  744.     {
  745.         if ($this->getProduct($product)->getOptions()) {
  746.             return true;
  747.         }
  748.  
  749.         $attributes = $this->getConfigurableAttributes($product);
  750.         if (count($attributes)) {
  751.             foreach ($attributes as $attribute) {
  752.                 /** @var Mage_Catalog_Model_Product_Type_Configurable_Attribute $attribute */
  753.                 if ($attribute->getData('prices')) {
  754.                     return true;
  755.                 }
  756.             }
  757.         }
  758.  
  759.         return false;
  760.     }
  761.  
  762.     /**
  763.      * Return product weight based on simple product
  764.      * weight or configurable product weight
  765.      *
  766.      * @param  Mage_Catalog_Model_Product $product
  767.      * @return decimal
  768.      */
  769.     public function getWeight($product = null)
  770.     {
  771.         if ($this->getProduct($product)->hasCustomOptions() &&
  772.             ($simpleProductOption = $this->getProduct($product)->getCustomOption('simple_product'))
  773.         ) {
  774.             $simpleProduct = $simpleProductOption->getProduct($product);
  775.             if ($simpleProduct) {
  776.                 return $simpleProduct->getWeight();
  777.             }
  778.         }
  779.  
  780.         return $this->getProduct($product)->getData('weight');
  781.     }
  782.  
  783.     /**
  784.      * Implementation of product specify logic of which product needs to be assigned to option.
  785.      * For example if product which was added to option already removed from catalog.
  786.      *
  787.      * @param  Mage_Catalog_Model_Product|null $optionProduct
  788.      * @param  Mage_Sales_Model_Quote_Item_Option $option
  789.      * @param  Mage_Catalog_Model_Product|null $product
  790.      * @return Mage_Catalog_Model_Product_Type_Configurable
  791.      */
  792.     public function assignProductToOption($optionProduct, $option, $product = null)
  793.     {
  794.         if ($optionProduct) {
  795.             $option->setProduct($optionProduct);
  796.         } else {
  797.             $option->getItem()->setHasConfigurationUnavailableError(true);
  798.         }
  799.         return $this;
  800.     }
  801.  
  802.     /**
  803.      * Retrieve products divided into groups required to purchase
  804.      * At least one product in each group has to be purchased
  805.      *
  806.      * @param  Mage_Catalog_Model_Product $product
  807.      * @return array
  808.      */
  809.     public function getProductsToPurchaseByReqGroups($product = null)
  810.     {
  811.         $product = $this->getProduct($product);
  812.         return array($this->getUsedProducts(null, $product));
  813.     }
  814.  
  815.     /**
  816.      * Get sku of product
  817.      *
  818.      * @param  Mage_Catalog_Model_Product $product
  819.      * @return string
  820.      */
  821.     public function getSku($product = null)
  822.     {
  823.         $simpleOption = $this->getProduct($product)->getCustomOption('simple_product');
  824.         if($simpleOption) {
  825.             $optionProduct = $simpleOption->getProduct($product);
  826.             $simpleSku = null;
  827.             if ($optionProduct) {
  828.                 $simpleSku =  $simpleOption->getProduct($product)->getSku();
  829.             }
  830.             $sku = parent::getOptionSku($product, $simpleSku);
  831.         } else {
  832.             $sku = parent::getSku($product);
  833.         }
  834.  
  835.         return $sku;
  836.     }
  837.  
  838.     /**
  839.      * Prepare selected options for configurable product
  840.      *
  841.      * @param  Mage_Catalog_Model_Product $product
  842.      * @param  Varien_Object $buyRequest
  843.      * @return array
  844.      */
  845.     public function processBuyRequest($product, $buyRequest)
  846.     {
  847.         $superAttribute = $buyRequest->getSuperAttribute();
  848.         $superAttribute = (is_array($superAttribute)) ? array_filter($superAttribute, 'intval') : array();
  849.  
  850.         $options = array('super_attribute' => $superAttribute);
  851.  
  852.         return $options;
  853.     }
  854.  
  855.     /**
  856.      * Check if Minimum Advertise Price is enabled at least in one option
  857.      *
  858.      * @param Mage_Catalog_Model_Product $product
  859.      * @param int $visibility
  860.      * @return bool|null
  861.      */
  862.     public function isMapEnabledInOptions($product, $visibility = null)
  863.     {
  864.         return null;
  865.     }
  866.  
  867.     /**
  868.      * Prepare and retrieve options values with product data
  869.      *
  870.      * @param Mage_Catalog_Model_Product $product
  871.      * @return array
  872.      */
  873.     public function getConfigurableOptions($product)
  874.     {
  875.         return Mage::getResourceSingleton('catalog/product_type_configurable')
  876.             ->getConfigurableOptions($product, $this->getUsedProductAttributes($product));
  877.     }
  878. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement