Tuesday, August 13, 2013

Programmatically change product attribute value in a store Magento


>> We have already known that magento has the save() method to save the data through model. We can save a product data programmatically with the product object. Here's a example:

            $_product = Mage::getModel('catalog/product')->load("PRODUCT_ID");
            $_product->setData('name','value');   OR   $_product->setName('value');
            $_product->save();
            // With this we can change the product name. But the save() method will save globally.

Now suppose you need to change a product visibility for a particular store. Then using this code you can change a particular attribute for a store without saving entire product attributes. Here's it :

            $_product = Mage::getModel('catalog/product')->load("PRODUCT_ID");
            $_product->setStoreId(1);
            $_product->setVisibility(1);
            $_product->getResource()->saveAttribute($_product, 'visibility');