1

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?

2
  • Looking at the models, I'm not convinced you actually can :S. The collection has setProductStoreId which allows you to set where the products will come from but joinUrlRewrite for example uses Mage::app()->getStore() which implies you can't easily join the URLs for a different store. You could try setStoreId on the model before getCollection but I'm not sure it'll have the desired effect. Mar 19, 2015 at 16:51
  • Seems it does work, I was just suffering from the side-effects of another issue (see answer below). In my case I'm loading the collection in a front-end block so I imagine store id is set implicitly. Not sure if it still works when using code outside of the store environment (e.g. in a cron job). Mar 19, 2015 at 17:25

2 Answers 2

1

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.

2
  • Aha, funny you fell afoul of that particular issue. We ran across something similar with products a couple of weeks ago. I believe it affects all EAV models. Mar 19, 2015 at 17:51
  • Thanks for the heads up, I didn't think to check my products... better go do that now :| Mar 20, 2015 at 8:51
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.