Example #1
0
 public function savePrices($attribute)
 {
     $this->_getWriteAdapter()->delete($this->_priceTable, $this->_getWriteAdapter()->quoteInto('product_super_attribute_id=?', $attribute->getId()));
     $prices = $attribute->getValues();
     foreach ($prices as $data) {
         $priceObject = new Varien_Object($data);
         $this->_getWriteAdapter()->insert($this->_priceTable, array('product_super_attribute_id' => $attribute->getId(), 'value_index' => $priceObject->getValueIndex(), 'is_percent' => $priceObject->getIsPercent(), 'pricing_value' => $priceObject->getPricingValue()));
     }
     return $this;
 }
Example #2
0
 /**
  * Save Options prices (Depends from price save scope)
  *
  * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute
  * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Type_Configurable_Attribute
  */
 public function savePrices($attribute)
 {
     $newValues = $attribute->getValues();
     $oldValues = array();
     $valueIndexes = array();
     $select = $this->_getWriteAdapter()->select()->from($this->_priceTable)->where('product_super_attribute_id=?', $attribute->getId());
     $query = $this->_getWriteAdapter()->query($select);
     while ($row = $query->fetch()) {
         $key = join('-', array($row['website_id'], $row['value_index']));
         $oldValues[$key] = $row;
     }
     $delete = array();
     $insert = array();
     $update = array();
     foreach ($newValues as $value) {
         $valueIndexes[$value['value_index']] = $value['value_index'];
     }
     if ($this->getCatalogHelper()->isPriceGlobal()) {
         foreach ($oldValues as $row) {
             if (!isset($valueIndexes[$row['value_index']])) {
                 $delete[] = $row['value_id'];
                 continue;
             }
         }
         foreach ($newValues as $value) {
             $valueObject = new Varien_Object($value);
             $key = join('-', array(0, $value['value_index']));
             $pricingValue = $valueObject->getPricingValue();
             if ($pricingValue == '' || is_null($pricingValue)) {
                 $pricingValue = null;
             } else {
                 $pricingValue = Mage::app()->getLocale()->getNumber($pricingValue);
             }
             // update
             if (isset($oldValues[$key])) {
                 $oldValue = $oldValues[$key];
                 $update[$oldValue['value_id']] = array('pricing_value' => $pricingValue, 'is_percent' => intval($valueObject->getIsPercent()));
             } else {
                 if (!empty($value['pricing_value'])) {
                     $insert[] = array('product_super_attribute_id' => $attribute->getId(), 'value_index' => $valueObject->getValueIndex(), 'is_percent' => intval($valueObject->getIsPercent()), 'pricing_value' => $pricingValue, 'website_id' => 0);
                 }
             }
         }
     } else {
         $websiteId = Mage::app()->getStore($attribute->getStoreId())->getWebsiteId();
         foreach ($oldValues as $row) {
             if (!isset($valueIndexes[$row['value_index']])) {
                 $delete[] = $row['value_id'];
                 continue;
             }
         }
         foreach ($newValues as $value) {
             $valueObject = new Varien_Object($value);
             $key = join('-', array($websiteId, $value['value_index']));
             $pricingValue = $valueObject->getPricingValue();
             if ($pricingValue == '' || is_null($pricingValue)) {
                 $pricingValue = null;
             } else {
                 $pricingValue = Mage::app()->getLocale()->getNumber($pricingValue);
             }
             // update
             if (isset($oldValues[$key])) {
                 $oldValue = $oldValues[$key];
                 if ($websiteId && $valueObject->getUseDefaultValue()) {
                     $delete[] = $oldValue['value_id'];
                 } else {
                     $update[$oldValue['value_id']] = array('pricing_value' => $pricingValue, 'is_percent' => intval($valueObject->getIsPercent()));
                 }
             } else {
                 if ($websiteId && $valueObject->getUseDefaultValue()) {
                     continue;
                 }
                 $insert[] = array('product_super_attribute_id' => $attribute->getId(), 'value_index' => $valueObject->getValueIndex(), 'is_percent' => intval($valueObject->getIsPercent()), 'pricing_value' => $pricingValue, 'website_id' => $websiteId);
             }
             $key = join('-', array(0, $value['value_index']));
             if (!isset($oldValues[$key])) {
                 $insert[] = array('product_super_attribute_id' => $attribute->getId(), 'value_index' => $valueObject->getValueIndex(), 'is_percent' => 0, 'pricing_value' => null, 'website_id' => 0);
             }
         }
     }
     if (!empty($delete)) {
         $where = $this->_getWriteAdapter()->quoteInto('value_id IN(?)', $delete);
         $this->_getWriteAdapter()->delete($this->_priceTable, $where);
     }
     if (!empty($update)) {
         foreach ($update as $valueId => $valueData) {
             $where = $this->_getWriteAdapter()->quoteInto('value_id=?', $valueId);
             $this->_getWriteAdapter()->update($this->_priceTable, $valueData, $where);
         }
     }
     if (!empty($insert)) {
         foreach ($insert as $valueData) {
             $this->_getWriteAdapter()->insert($this->_priceTable, $valueData);
         }
     }
     return $this;
 }