以编程方式更新自定义选项存储明智

时间:2015-04-16 08:03:14

标签: magento

在保存产品事件后使用observer.php进行任何产品添加/编辑时,以编程方式更新自定义选项存储明智

即。 我在我的magento项目中有两个商店

  1. 批发商
  2. 零售商
  3. 现在,我想添加单个产品并以不同的价格在两个商店中显示它。所以,我使用了带有自定义选项的简单产品。

    产品名称:T恤

    Wholesaler Store
    ------------------------
    Red : 50
    Green : 60
    Blue : 70
    
    Retailer Store
    -------------------
    Red : 150
    Green : 160
    Blue : 170
    

    现在,如果我想在Wholesaler Store和20Rs中添加10R。在零售商店,那我们该如何进行呢。

1 个答案:

答案 0 :(得分:0)

我得到了我的问题的解决方案"以编程方式更新自定义选项值存储"

步骤-1:     创建名为Addweb_Customtabs.xml文件的模块文件         应用程序的/ etc /模块/ Addweb_Customtabs.xml

    Here is my Addweb_Customtabs.xml file code

    <?xml version="1.0"?>
    <config>
        <modules>
           <Addweb_Customtabs>
              <active>false</active>
              <codePool>local</codePool>
           </Addweb_Customtabs>
        </modules>
    </config>

步骤-2:     创建config.xml文件         应用程序/代码/本地/ Addweb / Customtabs的/ etc / config.xml中

    Here is my config.xml file code

    <?xml version="1.0"?>
    <config>
        <modules>
           <Addweb_CustomTabs>
              <version>0.1.0</version>
           </Addweb_CustomTabs>
        </modules>
        <adminhtml>
           <events>
              <catalog_product_prepare_save>
                 <observers>
                     <Addweb_save_product_data>
                         <type>singleton</type>
                         <class>customtabs/observer</class>
                         <method>saveProductTabData</method>
                     </Addweb_save_product_data>
                 </observers>
              </catalog_product_prepare_save>
           </events>
        </adminhtml>
    </config>

步骤3:     创建Observer.php文件     app / code / local / Addweb / Customtabs / Model / Observer.php

Here is my Observer.php file code

public function saveProductTabData(Varien_Event_Observer $observer) {     

        $product = $observer->getEvent()->getProduct();
        $websites = Mage::app()->getWebsites(); 
      foreach ($websites as $_eachWebsite) {
        $_websiteId = $_eachWebsite->getWebsiteId();                 
        $website = Mage::app()->getWebsite($website_id);                 
        $website->getDefaultGroup(); 

        $websiteObj = new Mage_Core_Model_Website();
        $websiteObj->load($_websiteId);

        $storeIds = $websiteObj->getStoreIds(); 
        if (count($storeIds)) { 
            foreach ($storeIds as $storeId) { 
                $store_code = Mage::getModel('core/store')->load($storeId)->getCode();  

                $priceTable = 'catalog_product_option_type_price';
                $options = $product->getProductOptions();
                foreach ($options as $option) {
                    $values = $option->getValues();
                    foreach($values as $value) {
                        switch($object->getTitle()) { 
                            case "Red":
                                $oldPrice = $object->getPrice();
                                if($storeId == 1) {             // Assuming that Store ID : 1 for Retailer & 2 for Wholesaler
                                    $newPrice = $oldPrice + 10;
                                }
                                else if($storeId == 2) {
                                    $newPrice = $oldPrice + 20;
                                }
                                break;
                            case "Green":
                                $oldPrice = $object->getPrice();
                                if($storeId == 1) {
                                    $newPrice = $oldPrice + 10;
                                }
                                else if($storeId == 2) {
                                    $newPrice = $oldPrice + 20;
                                }
                                break;
                            case "Blue":
                                $oldPrice = $object->getPrice();
                                if($storeId == 1) {
                                    $newPrice = $oldPrice + 10;
                                }
                                else if($storeId == 2) {
                                    $newPrice = $oldPrice + 20;
                                }
                                break;
                        }

                        $select = $this->_getReadAdapter()->select()
                                ->from($priceTable, 'option_type_id')
                                ->where('option_type_id = ?', (int)$object->getId())
                                ->where('store_id = ?', (int)$storeId);
                            $optionTypeId = $this->_getReadAdapter()->fetchOne($select);

                        if ($optionTypeId) {
                            $bind  = array(
                                'price'         => $newPrice,
                                'price_type'    => $priceType
                            );
                            $where = array(
                                'option_type_id = ?'    => (int)$optionTypeId,
                                'store_id = ?'          => (int)$storeId
                            );

                            $this->_getWriteAdapter()->update($priceTable, $bind, $where);
                        } 
                        else {
                            $bind  = array(
                                'option_type_id'    => (int)$object->getId(),
                                'store_id'          => (int)$storeId,
                                'price'             => $newPrice,
                                'price_type'        => $priceType
                            );

                            $this->_getWriteAdapter()->insert($priceTable, $bind);
                        }
                    }
                }
            }
        }
    }
}