Mufaddal
Mufaddal

Reputation: 5381

How to filter category collection using storefilter?

I have one admin module and I want to get category collection storewise in that module so how can we filter collection using storefilter? addstorefilter is not working for category collection .

Upvotes: 1

Views: 13134

Answers (5)

Deepak Mankotia
Deepak Mankotia

Reputation: 4584

In Magento 1.9

$storeId=2;
$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();
                }

Upvotes: 0

augsteyer
augsteyer

Reputation: 1099

Props going to @xpoback for the correct answer. I wanted to reiterate the answer as there are a few answers that are somewhat correct and some that are not.

Short answer, use this:

$rootId = Mage::app()->getStore()->getRootCategoryId();
    $collection = Mage::getModel('catalog/category')
        ->getResourceCollection()
        ->addAttributeToSelect('*')
        ->addFieldToFilter('path', array('like' => "1/{$rootId}/%"))
        ->addIsActiveFilter();

For all who want to know the issues with other answers read on.

First off, the @Muffadal's answer is functional, but is extremely slow in terms of performance. You want to keep the data querying up to mySQL and not use php looping over a big collection (provided you have more than a few categories in your store).

The comment under that answer to use addPathsFilter('/' . $storecategoryid . '/') is also correct, but will be slower than using the LIKE operator in this case (see article)

@Serjio's method will not work in terms of distinguishing between different categories. What it will do is pull store specific translations. Example Store 1 has named the category Rings, Store 2 has named the same category My Rings. If we make the call from store 2 using his code, it will pull all categories no matter the Root, but will pull the Store 2's naming convention - My Rings

Upvotes: 0

xpoback
xpoback

Reputation: 275

$rootCategoryId = Mage::app()->getStore()->getRootCategoryId();
$categories = Mage::getModel('catalog/category')->getCollection();
$categories->addAttributeToFilter('path', array('like' => "1/{$rootCategoryId}/%"));

Upvotes: 1

Mufaddal
Mufaddal

Reputation: 5381

I have add below code and its working for me

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

from this code i get store root categoryid.

$category = Mage::getModel('catalog/category')->getCollection()->addFieldToFilter('is_active',array('eq' => 1))->load();

from this i get whole category collection

foreach($category as $cat)
{
  if($cat->getData('level')==2 && $cat->getData('parent_id')==$storecategoryid)
  { 
     echo 'my code';
  }
}

This way I get store category.

Upvotes: 1

Sergii Stotskyi
Sergii Stotskyi

Reputation: 5400

Use setStore or setStoreId methods:

$collection = Mage:getResourceModel('catalog/category_collection');

// or $collection = Mage::getModel('catalog/category')->getCollection();

$collection->setStoreId($myStoreId)
   ->load();

UPDATE: There is a simple script to check this:

<?php
require 'app/Mage.php';

Mage::app('admin');

$collection = Mage::getModel('catalog/category')->getCollection()
    ->addAttributeToSelect('name')
    ->setStoreId(2)
    ->load();

echo $collection->count(), "\n";
foreach ($collection as $item) {
   echo $item->getName(), "\n";
}

If you have 3 store views there will 3 storeIds. There are 0 - for admin store, 1 - for default store and 2 (or other value) for another store. I have just checked this and it works.

Upvotes: 1

Related Questions