Filtering Magento Category Collection by Store ID
If you’re trying to retrieve categories that belong only to your current store but are still getting data from other stores as well, there might be an issue with how you’re setting the store ID in your category collection.
Let’s break down your initial 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);
While you’re setting the store ID correctly using ->setStoreId(Mage::app()->getStore()->getId()), it seems that Magento is not honoring this filter when retrieving the category collection.
To investigate further, let’s look at the generated SQL query:
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)
Notice that the query is joining two sets of category attributes for each store (one default and one specific to the given store ID). However, it seems Magento is not properly filtering by the store_id in the join conditions.
To resolve this issue, you can filter the categories using the category path instead. Here’s an updated version of your code:
$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();
In this updated code, we first retrieve the root category ID for the current store. Then, we use the ->addFieldToFilter('path', array('like' => "%/{$categoryRootId}/%")) filter to include only categories that are descendants of the root category.
By using this approach, you can ensure that the retrieved category collection belongs exclusively to your current store.
Remember that this solution uses a workaround instead of directly filtering by store_id. If you still want to use direct store ID filtering, consider raising an issue on Magento’s GitHub repository or reaching out to their official support.
For more advanced Magento development tips and tricks, feel free to explore our blog at Laravelcompany.com.