public function saveOptions()
 {
     $helper = Mage::helper('customoptions');
     if (!$helper->isEnabled() || Mage::app()->getRequest()->getControllerName() != 'catalog_product' && Mage::app()->getRequest()->getControllerName() != 'adminhtml_catalog_product') {
         return parent::saveOptions();
     }
     $options = $this->getOptions();
     $post = Mage::app()->getRequest()->getPost();
     $productId = $this->getProduct()->getId();
     $storeId = $this->getProduct()->getStoreId();
     // bug magento fix
     if ($storeId > 0 && isset($post['copy_to_stores'][$storeId]) && $post['copy_to_stores'][$storeId] == 0) {
         return $this;
     }
     $relation = Mage::getSingleton('customoptions/relation');
     $connection = Mage::getSingleton('core/resource')->getConnection('core_write');
     $tablePrefix = (string) Mage::getConfig()->getTablePrefix();
     if (isset($post['image_delete'])) {
         $productOption = Mage::getModel('catalog/product_option');
         foreach ($post['image_delete'] as $optionId) {
             $connection->update($tablePrefix . 'catalog_product_option', array('image_path' => ''), 'option_id = ' . intval($optionId));
             $helper->deleteOptionFile(null, $optionId, false);
         }
     }
     foreach ($options as $option) {
         if (isset($option['option_id'])) {
             $connection->update($tablePrefix . 'catalog_product_option_type_value', array('default' => 0), 'option_id = ' . $option['option_id']);
             if (isset($option['default'])) {
                 foreach ($option['default'] as $value) {
                     $connection->update($tablePrefix . 'catalog_product_option_type_value', array('default' => 1), 'option_type_id = ' . $value);
                 }
             }
         }
     }
     if ($helper->isCustomerGroupsEnabled()) {
         $options = $this->getOptions();
         foreach ($options as $key => $option) {
             if (isset($option['customer_groups'])) {
                 $options[$key]['customer_groups'] = implode(',', $option['customer_groups']);
             }
         }
         $this->setOptions($options);
     }
     if ($helper->isStoreViewsEnabled()) {
         $options = $this->getOptions();
         foreach ($options as $key => $option) {
             if (isset($option['store_views'])) {
                 $options[$key]['store_views'] = implode(',', $option['store_views']);
             }
         }
         $this->setOptions($options);
     }
     // qnty_input, exclude_first_image
     $options = $this->getOptions();
     foreach ($options as $key => $option) {
         if (!isset($option['qnty_input']) || $this->getGroupByType($option['type']) != self::OPTION_GROUP_SELECT || $option['type'] == 'multiple' || $option['type'] == 'hidden') {
             $options[$key]['qnty_input'] = 0;
         }
         if (!isset($option['exclude_first_image']) || $this->getGroupByType($option['type']) != self::OPTION_GROUP_SELECT) {
             $options[$key]['exclude_first_image'] = 0;
         }
         if ($helper->isSkuNameLinkingEnabled() && (!isset($option['scope']['title']) || $option['scope']['title'] != 1) && (!isset($option['title']) || $option['title'] == '') && $this->getGroupByType($option['type']) != self::OPTION_GROUP_SELECT && $option['sku']) {
             $options[$key]['title'] = $helper->getProductNameBySku($option['sku'], $storeId);
         }
     }
     $this->setOptions($options);
     // original code m1510 parent::saveOptions();
     foreach ($this->getOptions() as $option) {
         $this->setData($option)->setData('product_id', $productId)->setData('store_id', $storeId);
         if ($this->getData('option_id') == '0') {
             $this->unsetData('option_id');
         } else {
             $this->setId($this->getData('option_id'));
         }
         $isEdit = (bool) $this->getId() ? true : false;
         if ($this->getData('is_delete') == '1') {
             if ($isEdit) {
                 $this->getValueInstance()->deleteValue($this->getId());
                 $this->deletePrices($this->getId());
                 $this->deleteTitles($this->getId());
                 $this->delete();
                 $helper->deleteOptionFile(null, $this->getId(), false);
             }
         } else {
             if ($this->getData('previous_type') != '') {
                 $previousType = $this->getData('previous_type');
                 //if previous option has dfferent group from one is came now need to remove all data of previous group
                 if ($this->getGroupByType($previousType) != $this->getGroupByType($this->getData('type'))) {
                     switch ($this->getGroupByType($previousType)) {
                         case self::OPTION_GROUP_SELECT:
                             $this->unsetData('values');
                             if ($isEdit) {
                                 $this->getValueInstance()->deleteValue($this->getId());
                             }
                             break;
                         case self::OPTION_GROUP_FILE:
                             $this->setData('file_extension', '');
                             $this->setData('image_size_x', '0');
                             $this->setData('image_size_y', '0');
                             break;
                         case self::OPTION_GROUP_TEXT:
                             $this->setData('max_characters', '0');
                             break;
                         case self::OPTION_GROUP_DATE:
                             break;
                     }
                     if ($this->getGroupByType($this->getData('type')) == self::OPTION_GROUP_SELECT) {
                         $this->setData('sku', '');
                         $this->unsetData('price');
                         $this->unsetData('price_type');
                         if ($isEdit) {
                             $this->deletePrices($this->getId());
                         }
                     }
                 }
             }
             // error protection
             if ($this->getGroupByType($this->getType()) != Mage_Catalog_Model_Product_Option::OPTION_GROUP_SELECT) {
                 if (is_null($this->getPrice())) {
                     $this->setPrice(0);
                 }
                 if (is_null($this->getPriceType())) {
                     $this->setPriceType('fixed');
                 }
                 if (is_null($this->getTitle())) {
                     $this->setTitle('price by sku');
                 }
             }
             $this->save();
             if (!isset($option['option_id']) || !$option['option_id']) {
                 $values = $this->getValues();
                 $option['option_id'] = $this->getId();
             }
             switch ($option['type']) {
                 case 'field':
                 case 'area':
                     if ($this->_uploadImage('file_' . $option['id'], $option['option_id'])) {
                         $this->setImagePath('options' . DS . $option['option_id'] . DS)->save();
                     }
                     break;
                 case 'drop_down':
                 case 'radio':
                 case 'checkbox':
                 case 'multiple':
                 case 'swatch':
                 case 'multiswatch':
                 case 'hidden':
                     break;
                 case 'file':
                 case 'date':
                 case 'date_time':
                 case 'time':
                     // no image
                     if (isset($option['option_id'])) {
                         $helper->deleteOptionFile(null, $option['option_id'], false);
                         $this->setImagePath('')->save();
                     }
                     break;
             }
         }
     }
     //eof foreach()
     // end original code m1510 parent::saveOptions();
     if ($productId && isset($post['affect_product_custom_options'])) {
         if (isset($post['customoptions']['groups'])) {
             $postGourps = $post['customoptions']['groups'];
         } else {
             $postGourps = array();
         }
         $groupModel = Mage::getSingleton('customoptions/group');
         $groups = $relation->getResource()->getGroupIds($productId, false);
         if (isset($groups) && is_array($groups) && count($groups) > 0) {
             $keepOptionsFlag = isset($post['general']['keep_options']) ? $post['general']['keep_options'] : 0;
             foreach ($groups as $id) {
                 if (count($postGourps) == 0 || !in_array($id, $postGourps)) {
                     if ($keepOptionsFlag) {
                         $this->unlinkProductOptions($id, $productId);
                     } else {
                         $this->removeProductOptions($id, $productId);
                     }
                     $relation->getResource()->deleteGroupProduct($id, $productId);
                 } else {
                     $relationOptionIds = $relation->getResource()->getOptionIds($id, $productId);
                     if ($relationOptionIds && is_array($relationOptionIds) && count($relationOptionIds) > 0) {
                         foreach ($relationOptionIds as $opId) {
                             $check = Mage::getModel('catalog/product_option')->load($opId)->getData();
                             if (empty($check)) {
                                 $relation->getResource()->deleteOptionProduct($id, $productId, $opId);
                             }
                         }
                     }
                     if (count($postGourps) > 0) {
                         $key = array_search($id, $postGourps);
                         unset($postGourps[$key]);
                     }
                 }
             }
         }
         if (count($postGourps) > 0) {
             foreach ($postGourps as $templateId) {
                 if (!empty($templateId)) {
                     $group = $groupModel->load($templateId);
                     $optionsHash = unserialize($group->getData('hash_options'));
                     $this->saveProductOptions($optionsHash, array(), array($productId), $group, 1, 'product');
                 }
             }
         } else {
             // save absolutePrice, absoluteWeight and skuPolicy
             $absolutePrice = isset($post['general']['absolute_price']) ? $post['general']['absolute_price'] : 0;
             $absoluteWeight = isset($post['general']['absolute_weight']) ? $post['general']['absolute_weight'] : 0;
             $skuPolicy = isset($post['general']['sku_policy']) ? $post['general']['sku_policy'] : 0;
             $this->updateProductFlags($productId, $absolutePrice, $absoluteWeight, $skuPolicy);
         }
     }
     return $this;
 }
Beispiel #2
0
 /**
  * Save options.
  *
  * @return Mage_Catalog_Model_Product_Option
  */
 public function saveOptions()
 {
     /* {#AITOC_COMMENT_END#}
              Mage::app()->removeCache('aitsys_used_product_count_aitcg_count');
              
              if($this->getProduct()->getData('status') == Mage_Catalog_Model_Product_Status::STATUS_ENABLED) {
                 
                 $bHasAitOption = false;
                 $updateOptions = array();
                 foreach ($this->getOptions() as $iKey => $aOption)
                 {
                     if( Mage::helper('aitcg/options')->checkAitOption( $aOption ) ) {
                         if($aOption['option_id'] == 0) {
                             $bHasAitOption = true;
                             break;
                         }
                         $updateOptions[ $aOption['option_id'] ] = $aOption['is_delete'];
                     }
                 }
     
                 if($bHasAitOption == false) {
                     foreach ($this->getProduct()->getOptions() as $iKey => $aOption)
                     {
                         if(Mage::helper('aitcg/options')->checkAitOption( $aOption )) {
                             if(!isset($updateOptions[ $aOption->getId() ]) || $updateOptions[ $aOption->getId() ]!=1) {
                                 $bHasAitOption = true;
                                 break;
                             }
                         }
                     }
                 }
     
                 if($bHasAitOption) {
                     $performer = Aitoc_Aitsys_Abstract_Service::get()->platform()->getModule('Aitoc_Aitcg')->getLicense()->getPerformer();
                     $ruler = $performer->getRuler();
                     $ruler->checkRuleAdd($this->getProduct(), true);
                 }
     
                 
             }
             {#AITOC_COMMENT_START#} */
     parent::saveOptions();
 }