4

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?

  • : can i get the working code? – zus Jan 23 '18 at 6:20
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();
  • 2
    As mentioned on a previous answer, calling load directly on a collection is not desirable. – philwinkle Oct 14 '13 at 17:32
  • 5
    Why this answer accepted as correct? Question is about category collection, not product mentioned here. – mageUz Oct 14 '13 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 '13 at 0:46
20

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();
  • 1
    this should be accepted – Hashid Nov 24 '14 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? – Doni Wibowo Oct 12 '17 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();


                }
  • 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 '17 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.