开发者

Programmatically change product attribute at store view level

开发者 https://www.devze.com 2023-03-19 03:36 出处:网络
I\'m sorry if this question is trivial but I\'ve been stru开发者_开发技巧ggling 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 defaul

I'm sorry if this question is trivial but I've been stru开发者_开发技巧ggling 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);


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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消