Hi All,
Programmatically Update Custom Option Store Wise while any product Add / Edit Using observer.php after save product event
i.e.
I have two store in my magento project
1/ Wholesaler
2/ Retailer
Now, i want to add single product and display it in both store with different price. So, i have used Simple Product with Custom Option.
Product Name : T-Shirt
Wholesaler Store
------------------------
Red : 50
Green : 60
Blue : 70
Retailer Store
-------------------
Red : 150
Green : 160
Blue : 170
Now, if i want to add 10Rs in Wholesaler Store and 20Rs. in Retailer store then how can we proceed on it.
Thanks,
Snehal
Have you tried changing the Configuration Scope and editing the inventory for each store? I assume that you're trying to have different inventory levels, if not I completely got lost in your question.
I Got the Solution for my question "Programmatically Update Custom Option value store wise"
Step - 1 :
Create module file named Addweb_Customtabs.xml file
app/etc/module/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>
Step - 2 :
Create config.xml file
app/code/local/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>
Step - 3 :
Create Observer.php file
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);
}
}
}
}
}
}
}