Magento & # 8594; addCategoryFilter - filter a collection of products by root category

About Usage β†’ addCategoryFilter in a Product Collection. Why is it impossible to filter the root category? I have 2 stores, both with different root categories. I want to show a list of best sellers on my homepage. But I can’t filter products by root category, under this subcategory.

So, on my homepage all products from both stores appear. I can filter ok with any subcategories. For example, my second root category has an identifier of 35. If I try to filter this, I get each product from both roots. But the first subcategory under this root is ID 36, and filtering by this rule works correctly, showing only those products. My call is as follows (simplified):

$_category = Mage::getModel('catalog/category')->load(35); $_testproductCollection = Mage::getResourceModel('catalog/product_collection') ->addCategoryFilter($_category) ->addAttributeToSelect('*'); $_testproductCollection->load(); foreach($_testproductCollection as $_testproduct){ echo $this->htmlEscape($_testproduct->getName())."<br/>"; }; 

Does anyone know why this is not working? Or is there another way to filter by root category?

UPDATE: I still had no luck. It seems very buggy - adding a category filter using the root category only works sometimes, you need to add all the products to the root directory, then save and then delete everything that should not be in this root cat, and then save it again. But if you re-index, you will get all products again. If I get the sql query from my collection call, I get the following:

 SELECT `e`.*, `cat_index`.`position` AS `cat_index_position`, `price_index`.`price`, `price_index`.`tax_class_id`, `price_index`.`final_price`, IF(`price_index`.`tier_price`, LEAST(`price_index`.`min_price`, `price_index`.`tier_price`), `price_index`.`min_price`) AS `minimal_price`, `price_index`.`min_price`, `price_index`.`max_price`, `price_index`.`tier_price` FROM `catalog_product_entity` AS `e` INNER JOIN `catalog_category_product_index` AS `cat_index` ON cat_index.product_id=e.entity_id AND cat_index.store_id='2' AND cat_index.category_id='35' INNER JOIN `catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND price_index.website_id = '1' AND price_index.customer_group_id = 0 

As you can see, the category is listed there, so why the filter does not work?

+6
magento
source share
5 answers

Well, I think this works, did not experience too much, but it seems to have done the trick. You need to first get your identifier for the root category of stores, and then join some fields so that you have access to the products "category_id" and then filter with this:

 $_rootcatID = Mage::app()->getStore()->getRootCategoryId(); $_testproductCollection = 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('*'); $_testproductCollection->load(); foreach($_testproductCollection as $_testproduct){ echo $this->htmlEscape($_testproduct->getName())."<br/>"; }; 
+16
source share

I had a similar question and in the end I answered my question: https://magento.stackexchange.com/questions/24436/product-collection-for-default-category

 $store_id = 1; $root_category_id = Mage::app()->getStore(Mage::app()->getStore()->getId())->getRootCategoryId(); $collection = Mage::getResourceModel('catalog/product_collection')->setStoreId($store_id); $model = Mage::getModel('catalog/product')->setStoreId($store_id); $category_model = Mage::getModel('catalog/category'); $category = $category_model->load($root_category_id); $category->setIsAnchor(true); $collection->addCategoryFilter($category); echo $collection->getSize(); // example to show you a count of products 
+2
source share

Try the following:

$_testproductCollection = Mage::getResourceModel('catalog/product_collection') ->setVisibility(array(self::VISIBILITY_IN_CATALOG, self::VISIBILITY_BOTH)) ->addCategoryFilter($_category) ->addAttributeToSelect('*');

0
source share

He did not try, but I have two ideas:

(1) You can try to add an additional store filter to the category:

 $collection = Mage::getModel('catalog/category') ->getCollection() ->addFieldToFilter('store_id', Mage::app()->getStore()->getId()) ->addIdFilter(35); foreach ($collection as $item) { ... } 

This is not very clean because category_id is hard-coded.

(2) You can also try addPathFilter() in the collection (I don’t know the root category by heart, maybe just β€œ/”, but you can find it in the database or use getPath () the loaded root category). You might also need to use a storage filter.

0
source share

:

  • Magento Version 1.7.0.2
  • only one store
  • edited categories in global mode
  • deleted old root category. Thus, old products did not have categories.
  • The catalog_category_product_index table remains in poor condition (even after reindexing) and is displayed in the new root category!

I create a helper method:

  public function getCategoriesId($parent = -1,$recursionLevel = -1, $asCollection = true) { if($parent === -1){ $categories = Mage::helper('catalog/category')->getStoreCategories(false, true); }else{ $recursionLevel = ($recursionLevel<0) ? max(0, (int) Mage::app()->getStore()->getConfig('catalog/navigation/max_depth')) : $recursionLevel; $categories = $category->getCategories($parent, $recursionLevel, false, $asCollection, true); } $ids = array(); foreach($categories as $category){ if($category->getId()){ $ids[] = $category->getId(); } } return $ids; } 

I used the result to filter categories like jazzhand, written:

  ->addAttributeToFilter('category_id', array('in' => $_rootcatID)) 

I am not sure if it is not needed only for the root category.

0
source share

All Articles