Writing Magento category attributes at the store view level

Hello! I’m new at BigBridge, and at Magento as well, so I am still learning a lot every day. Sometimes it takes me hours to find something out. Today’s subject is no different. At the end I have usually found one or two pages on the internet that explain the subject well. In this case it is different. I had to dive into Magento’s source code to understand how it’s done. That seemed like a nice opportunity for a first blog 🙂

My problem was that I simply wanted to be able to write a category attribute for a specific store view.

The attribute is called ‘is_active’, the store view’s id is stored in the variable $storeId, the category’s id in $categoryId and this seemed the way to do it:

But it doesn’t work. Not just ‘is_active’ is now stored at store view level. All other attributes are stored at this level as well. This is not what I wanted. I want to have the other attributes remain unchanged.

Category data can be set at two levels: the global level and the store view level (which is called “store” in Magento’s code). When loading a category, Magento defaults to the values at the global level (internally this is store_id 0).

By setting the store id before loading the category’s data, Magento loads the attribute values at the global level first and overwrites it with the attribute values that were stored at the store view level. The object now contains a mix of global and store view values. After we have set  is_active to 1, the attribute values looks like this.


Just saving this object again causes all values to be written at the store view level.


If we now were to change the name of the category to “Jackets” at a global level, and then view the category in some store view, the old name of the category would still be shown.

That’s not good. Now, Magento contains a method that allows you to save a single attribute value at store view level. But this doesn’t update the category index (the “Category Flat Data”), so that would need to be done separately. By saving the object properly we make sure all the normal event handlers are called. An exception to this rule should only be made when processing large amounts of data.

I decided to find out how Magento does this itself, by checking out the source code of the admin panel of category management. In Mage_Adminhtml_Catalog_CategoryController::saveAction() you’ll find that all attributes whose ‘Use Default Value’ was checked are set to false

This means that we can just set the attributes we don’t want to store at the store view level to false. But which attributes are those? If we set too many attributes to false we remove data that was present before. That’s bad.

In Mage_Adminhtml_Block_Catalog_Form_Renderer_Fieldset_Element::canDisplayUseDefault() it is determined if the checkbox ‘Use Default Value’ should be shown. This line stands out

Finally we find in Mage_Adminhtml_Block_Catalog_Form_Renderer_Fieldset_Element::usedDefault() when the checkbox should be turned on. Here this line stands out

When we combine these pieces of code we get the following snippet to update only a single attribute at store view level

That’s all for now! I hope this of any use to you. Friendly feedback is welcome 🙂

Geef een antwoord

Het e-mailadres wordt niet gepubliceerd.