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?