Hervé Guétin
Hervé Guétin

Reputation: 4392

Programmatically change product attribute at store view level

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);

Upvotes: 19

Views: 28867

Answers (3)

jaro
jaro

Reputation: 31

note when loading product with data for some store view, also default values get loaded. saving such product will save default values as store values (thus unset "Use Default Value" for fields) i ended up with following function to clean-up product data from default values

public static function _removeDefaults($item) {
    static $attributeCodes = null;
    if($attributeCodes == null) {
        $attributes = Mage::getSingleton('eav/config')
            ->getEntityType(Mage_Catalog_Model_Product::ENTITY)->getAttributeCollection();
        $attributeCodes = array();
        foreach($attributes as $attribute) {
            $ac = $attribute->getAttributeCode();
            if(in_array($ac, array('sku','has_options','required_options','created_at','updated_at','category_ids'))) {
                continue;
            }
            $attributeCodes[] = $ac;
        }
    }
    foreach($attributeCodes as $ac) {
        if(false == $item->getExistsStoreValueFlag($ac)) {
            $item->unsetData($ac);
        }
    }
}

remember to send only product loaded for some store view

Upvotes: 3

Hervé Guétin
Hervé Guétin

Reputation: 4392

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();

Upvotes: 46

ʍǝɥʇɐɯ
ʍǝɥʇɐɯ

Reputation: 4022

You need to set the current store to admin at the top of your code block:

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

Upvotes: 12

Related Questions