Question

I have found plenty of questions and answers regarding how to load categories from a particular store, but this is a different issue.

My problem is, having loaded the categories that I want, their attribute data is always the default / global value.

Mage::getModel('catalog/category')->getCollection()
    ->addAttributeToSelect(array('name', 'description')
    ->addAttributeToFilter('is_active', 1)
    ->addIdFilter('31,32,33,34') // has to be a string, array doesn't work!
    ->setStoreId(2);

In the above example, the names and descriptions that I get back are the default values, not the ones set for store ID 2. It looks like setStoreId does nothing.

I know that the categories themselves are not connected with a particular store view, but the attributes are: in the admin panel, you can set values per store view, per website and globally. I've set them per store view.

I've tried ->addStoreFilter(2) but Mage_Catalog_Model_Category_Collection doesn't support that, I get a PHP fatal error. Looks like it's a product-specific filter.

Does anyone know what I'm missing?

Was it helpful?

Solution

I believe I've found the answer.

In fact the collection, under normal circumstances, works fine. It turned out that in my case, some required attributes weren't set, which was causing this side-effect.

Fun fact: required attributes are not enforced when adding categories via the API.

Somehow, if the "Available Product Listing Sort By" is undefined at "Default" level (and I suspect any required attribute would be the same), then store-specific values are not loaded.

Funny how store-specific data is affected by omissions in default-level data. I did try setting this attribute at the store level, so that at store level the category data was complete, but it still didn't help. Only fixing it at default level solved my problem.

OTHER TIPS

I got the correct storeview values by emulating the specific storeview by hand using the class Mage_Core_Model_App_Emulation

/** @var \Mage_Core_Model_App_Emulation $appEmulation */
                                $appEmulation = \Mage::getSingleton('core/app_emulation');

                                //Start environment emulation of the specified store
                                $initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId);

                                /** @var \Mage_Catalog_Model_Category $_category */
                                $_category = \Mage::getModel("catalog/category")->load($catId);
                                $_category->load($catId); //Load again because some attributes wont be visible/available
                                $categoryName               = $_category->getName(); //Get the storeview specific categoryname


// ....

                                //Stop environment emulation and restore original store
                                $appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top