Set Category Attribute Value for Store Programmatically

Set Category Attribute Value for Store Programmatically

Compatibility: Magento 1

In this article we will demonstrate how to save a value of attribute of a category for specific store.

In our example, we will change name of category (whos ID is 4), from “Shirts” to “French Shirts”, only for specific store view (whos ID is 3) (the French store view).

Note: If you want to set the attribute value for Default scope (all store views), use setStoreId(0).

    This code is very efficient, because of two reasons:

  • The code doesn’t load the category
  • The code doesn’t save the category, but only specific attribute value.
/*
* @var Mage_Catalog_Model_Category
*/
$categorySingleton = Mage::getSingleton('catalog/category');
$categorySingleton->setId(4);
$categorySingleton->setName('French Shirts');
$categorySingleton->setStoreId(3);

Mage::getModel('catalog/category')->getResource()->saveAttribute($categorySingleton, 'name');

Tested on: Magento CE 1.7.0.2, 1.8.0.1

Note: this script will not work properly through sql install script, but either through data install script or external php script which bootstraps Magento (like the Magento shell scripts which are located in “shell” directory).