Magento - store_id filter category resource collection

I am trying to pull out a category that belongs only to the current store, but does not seem to work. Can anyone see any problems 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); 

This is getting data from store_id 0, but I only want 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 the storage filter, I just added a path filter that solved the problem, but still wanted to know if the storage filter worked.

 $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(); 
0
magento
source share
6 answers

I know this is an old question, but if someone is looking for an answer, as I just was:

 ->addStoreFilter( {storeID} ) 

did it for me ...

+2
source share

I spent a lot of time ... this work works 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 multi-line :)

setStoreId - does not work, you can delete

+2
source share

Hi, the following code can help you.

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

OR

 $storeId =Mage::app()->getStore()->getStoreId(); $collection->setStoreId($storeId); $collection->addStoreFilter($storeId); 
0
source share

try it

 $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('*'); 
0
source share

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

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

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(); } 
0
source share

All Articles