Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically change product attribute at store view level

Tags:

magento

I'm sorry if this question is trivial but I've been struggling to find what I am doing wrong here. I am trying to change the value of an attribute on a store view level but the default is also changed whereas it shouldn't be. Of course, this attribute is set up to be "store-view-scoped". To keep it simple, I've tried with the product name. No success.

Below are unsuccessful tests I've tried...

Do you see what I am doing wrong here?

Many thanks.


My tries :

$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$product->setStoreId(STORE_ID)->setName('new_name')->save();

$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$product->setStoreId(STORE_ID)->setStore(STORE_CODE)->setName('new_name')->save();

$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$product->setStoreId(STORE_CODE)->setName('new_name')->save();

$product = Mage::getModel('catalog/product')->setStoreId(STORE_ID)->load(PRODUCT_ID);
$product->setName('new_name')->save();

$product = Mage::getModel('catalog/product')->setStoreId(STORE_ID)->load(PRODUCT_ID);
$product->setStoreId(STORE_ID)->setName('new_name')->save();

I even tried by adding the line below before the product model load...

Mage::app()->setCurrentStore(STORE_ID);
like image 359
Hervé Guétin Avatar asked Jul 07 '11 17:07

Hervé Guétin


People also ask

How to Update product attribute using patch data Magento 2?

Need to create a Setup folder in your module with the Patch\Data folder inside it and create any PHP file to update title. In the above code, We need to call, updateAttribute() method to update attribute. The first parameter is an Entity type, we have a product entity type.

How to Update custom attribute in Magento 2?

If there is an attribute with store view scope. After updating these files, update your custom product attribute's scope in your custom module in your Magento 2 instance by terminal run following command in your magento2 root directory. is_global flag is used instead of global in updating the scope of an attribute.


1 Answers

So here is the complete snippet to change attribute value for a specific product attribute on a specific store view. Example with the product name :

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$product->setStoreId(STORE_ID)->setName('my_new_product_name')->save();

And as an additional answer, one could be interested in changing the attribute value to the default one. In this case, the argument 'false' must be passed to the setAttribute :

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$product->setStoreId(STORE_ID)->setName(false)->save();
like image 62
Hervé Guétin Avatar answered Nov 08 '22 21:11

Hervé Guétin