getProduct(); $this->_setChangedValues($product); foreach ($this->_attributeCodes as $attributeCode) { $attributeValues = $product->getData($attributeCode); if ($attributeValues != null) { $attribute = $product->getResource()->getAttribute($attributeCode); $options = $attribute->getSource()->getAllOptions(false); $customOptionData = $this->prepareOptionData($options, $attributeValues, $product->getSku()); if ($customOptionData != null) { $oldOptions = $product->getOptionInstance()->getOptions(); if ($this->_checkIsChanged($attributeCode) || sizeof($product->getOptions())==0) { foreach ($oldOptions as $key => $option) { if (strtolower($option['title']) == strtolower('Size') && $option['type'] == 'drop_down') { $oldOptions[$key]['is_delete'] = 1; $product->getOptionInstance()->addOption($oldOptions[$key]); } } $product->setCanSaveCustomOptions(true); $product->getOptionInstance()->addOption($customOptionData); $product->setHasOptions(true); } } } } return $this; } public function addCustomOptions2Duplicate($observer) { // array('current_product' => $this, 'new_product' => $newProduct); /** @var $product Mage_Catalog_Model_Product */ $product = $observer->getCurrentProduct(); /** @var $newProduct Mage_Catalog_Model_Product */ $newProduct = $observer->getNewProduct(); foreach ($this->_attributeCodes as $attributeCode) { $attributeValues = $product->getData($attributeCode); if ($attributeValues != null) { $attribute = $product->getResource()->getAttribute($attributeCode); $options = $attribute->getSource()->getAllOptions(false); $customOptionData = $this->prepareOptionData($options, $attributeValues, $product->getSku()); $oldOptions = $product->getOptionInstance()->getOptions(); if ($this->_checkIsChanged($attributeCode) || sizeof($product->getOptions())==0) { foreach ($oldOptions as $key => $option) { if (strtolower($option['title']) == strtolower('Size') && $option['type'] == 'drop_down') { $oldOptions[$key]['is_delete'] = 1; $product->getOptionInstance()->addOption($oldOptions[$key]); } } $newProduct->setCanSaveCustomOptions(true); $newProduct->getOptionInstance()->addOption($customOptionData); $newProduct->setHasOptions(true); } } } return $this; } protected function _setChangedValues($product) { $this->_newValues = array_diff_assoc($product->getData(), $product->getOrigData()); $this->_oldValues = array_diff_assoc($product->getOrigData(), $product->getData()); return $this; } protected function _checkIsChanged($key) { $result = false; if (isset ($this->_newValues[$key]) && isset($this->_oldValues[$key])) { $result = sizeof(array_diff_assoc($this->_newValues[$key], explode(',', $this->_oldValues[$key]))) != 0 ? true : false; } return $result; } protected function prepareOptionData($optionsData, $attributeValues, $sku) { $selectedValues = array(); foreach ($optionsData as $key => $value) { if (in_array($value['value'], array_values($attributeValues))) { $selectedValues[] = $optionsData[$key]; } } if (count($selectedValues) > 0) { $options = array(); for ($i = 0; $i < count($selectedValues); $i++) { $options[$i] = array('is_delete' => '', 'title' => $selectedValues[$i]['label'], 'price_type' => 'fixed', 'price' => '', 'sort_order' => $i, 'sku' => $sku . '-' . $selectedValues[$i]['value'], ); } $optionData = array( 'is_delete' => 0, 'is_require' => true, 'previous_group' => '', 'title' => 'Size', 'type' => 'drop_down', 'sort_order' => 1, 'values' => $options, ); return $optionData; } return null; } }