Damodar Bashyal
Damodar Bashyal

Reputation: 941

Magento - Filter category resource collection by store_id

I am trying to pull category that belongs to current store only but it doesn't seem to work. Can anyone see any issue in my code?

$categoryCollection = Mage::getResourceModel('catalog/category_collection')
  ->setStoreId(Mage::app()->getStore()->getId())
  ->addFieldToFilter('include_in_menu', array('eq' => 1))
  ->addFieldToFilter('is_active', array('eq' => 1))
  ->addFieldToFilter('level', array('eq' => 2))
  ->addAttributeToSelect(array('name','url_path','image','description'))
  ->setOrder('position', 'asc');

$categoryCollection->printLogQuery(true);

That's getting data from store_id 0 as well, but i want from only store_id 2

SELECT `e`.*, IF(at_include_in_menu.value_id > 0, at_include_in_menu.value, at_include_in_menu_default.value) AS `include_in_menu`, IF(at_is_active.value_id > 0, at_is_active.value, at_is_active_default.value) AS `is_active` FROM `catalog_category_entity` AS `e`
 INNER JOIN `catalog_category_entity_int` AS `at_include_in_menu_default` ON (`at_include_in_menu_default`.`entity_id` = `e`.`entity_id`) AND (`at_include_in_menu_default`.`attribute_id` = '67') AND `at_include_in_menu_default`.`store_id` = 0
 LEFT JOIN `catalog_category_entity_int` AS `at_include_in_menu` ON (`at_include_in_menu`.`entity_id` = `e`.`entity_id`) AND (`at_include_in_menu`.`attribute_id` = '67') AND (`at_include_in_menu`.`store_id` = 2)
 INNER JOIN `catalog_category_entity_int` AS `at_is_active_default` ON (`at_is_active_default`.`entity_id` = `e`.`entity_id`) AND (`at_is_active_default`.`attribute_id` = '42') AND `at_is_active_default`.`store_id` = 0
 LEFT JOIN `catalog_category_entity_int` AS `at_is_active` ON (`at_is_active`.`entity_id` = `e`.`entity_id`) AND (`at_is_active`.`attribute_id` = '42') AND (`at_is_active`.`store_id` = 2) WHERE (`e`.`entity_type_id` = '3') AND (IF(at_include_in_menu.value_id > 0, at_include_in_menu.value, at_include_in_menu_default.value) = 1) AND (IF(at_is_active.value_id > 0, at_is_active.value, at_is_active_default.value) = 1) AND (`e`.`level` = 2)

UPDATE

Instead of store filter, I just added path filter which solved the issue but still would love to know if store filter works.

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

$categoryCollection = Mage::getResourceModel('catalog/category_collection')
    ->addFieldToFilter('path', array('like' => "%/{$categoryRootId}/%"))
    ->addFieldToFilter('include_in_menu', array('eq' => 1))
    ->addFieldToFilter('is_active', array('eq' => 1))
    ->addFieldToFilter('level', array('eq' => 2))
    ->addAttributeToSelect(array('name','url_path','image','description','store_id'))
    ->setOrder('position', 'asc')
    ->load();

Upvotes: 0

Views: 15365

Answers (6)

Markus
Markus

Reputation: 31

i know it is an old question, but if someone is looking for the answer as i just was:

->addStoreFilter( {storeID} )

did it for me...

Upvotes: 3

Vishal
Vishal

Reputation: 900

Hello check following code may be help you

->addFieldToFilter('store_id', Mage::app()->getStore()->getId());

OR

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

$collection->setStoreId($storeId);

$collection->addStoreFilter($storeId);

Upvotes: 0

Deepak Mankotia
Deepak Mankotia

Reputation: 4564

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

xpoback
xpoback

Reputation: 275

Neither setStoreId() nor addAttributeToFielter('store_id', $storeId) don't work because there's no store_id in the category tables. The code below works perfectly:

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

Upvotes: 0

Alex
Alex

Reputation: 767

I spent a lot of time.... this exaple work for me...

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

$rootpath = Mage::getModel('catalog/category')
                    ->setStoreId($store)
                    ->load($rootCategoryId)
                    ->getPath();

$categories = Mage::getModel('catalog/category')->setStoreId($store)
                    ->getCollection()
                    ->addAttributeToSelect('*')
                    ->addAttributeToFilter('path', array("like"=>$rootpath."/"."%")); 

fix for multistore :)

setStoreId - not work, can remove

Upvotes: 2

Palanikumar
Palanikumar

Reputation: 1746

Try this

$categoriesCollection = Mage::getModel('catalog/category')
->getCollection()
->setStoreId(1) 
->addFieldToFilter('include_in_menu', array('eq' => 1))
->addFieldToFilter('level', array('eq' => 2))
->addFieldToFilter('is_active', array('eq'=>'1'))
->setOrder('position', 'asc')
->addAttributeToSelect('*');

Upvotes: 0

Related Questions