6

With flat tables turned off, how can I get a category collection from a specific store view ?

I have tried the following:

Mage::getModel('catalog/category')->getCollection()->setStoreId(3);

But it only returns the collection for the default store?

What is not correct with my example?

1
  • : can i get the working code?
    – zus
    Jan 23, 2018 at 6:20

4 Answers 4

3

Instead of store filter, try with root category filter.

$rootcatID = Mage::app()->getStore()->getRootCategoryId();

$productCollection = Mage::getResourceModel('catalog/product_collection')
->joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left')
->addAttributeToFilter('category_id', array('in' => $rootcatID))
->addAttributeToSelect('*');

$productCollection->load();
3
  • 2
    As mentioned on a previous answer, calling load directly on a collection is not desirable.
    – philwinkle
    Oct 14, 2013 at 17:32
  • 6
    Why this answer accepted as correct? Question is about category collection, not product mentioned here.
    – mageUz
    Oct 14, 2013 at 17:45
  • 2
    @Marty: Did this answer really solve your questions? You might want to edit the question to make it clearer?
    – Alex
    Oct 18, 2013 at 0:46
24

In Magento store has relationship with only root category and all categories belong to this root category are belong to particular store. To load store categories you should use store root category id, and you can load all categories of this root. See these:

    $rootId     = Mage::app()->getStore($storeId)->getRootCategoryId();
    $categories = Mage::getModel('catalog/category')
        ->getCollection()
        ->addFieldToFilter('path', array('like'=> "1/$rootId/%"));

    echo $categories->count();
2
  • 2
    this should be accepted
    – Hashid
    Nov 24, 2014 at 6:02
  • this works well but left me a question. What's the point of magento having setStore if it doesn't automatically filter collection by its root category ID for that current store? Oct 12, 2017 at 3:26
4

In Magento 1.9

$rootCategoryId = Mage::app()->getStore($storeId)->getRootCategoryId();


            $categories = Mage::getModel('catalog/category')
                ->getCollection()
                ->setStoreId($storeId)
                ->addFieldToFilter('is_active', 1)
                ->addAttributeToFilter('path', array('like' => "1/{$rootCategoryId}/%"))
                ->addAttributeToSelect('*');

                foreach($categories as $categorie)
                {
                    $catid=$cat->getId();                   
                    $catname=$categorie->getName();
                    $catp=catp$categorie->getParent_id();


                }
1
  • so what is causing the correct filter here? Is t the store specific rootCategoryId || is it ->setStoreId($storeId) in the collection?
    – snh_nl
    Jan 27, 2017 at 19:26
0

Maybe this could help somebody, but with a little digging and testing i came up with the following:

$catCollection = Mage::getModel('catalog/category')->getCollection()
    ->setStoreId(9) //where 9 should be your store id
    ->addFieldToFilter('is_active', 1);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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