dotty
dotty

Reputation: 41433

addCategoryFilter doesn't work at all in Magento

I have a category called "New" which has 14 products within it.

I'm using Mage::getModel('catalog/category')->load(6); to load this category (6 is the ID within the admin).

If I call echo $category->getProductCount(); it correctly displays 14 (the amount of products in this category).

Now, I'm grabbing the products from the category with

$products = Mage::getModel('catalog/product')
    ->setStoreId( Mage::app()->getStore()->getId() )
    ->getCollection()
    ->addAttributeToFilter('visibility', $visibility)
    ->addCategoryFilter($category)
    ->addAttributeToSelect("*")
    ->setOrder('name','asc');

to display all 14 of these products, then using a simple foreach() loop, but when I loop through the $products object, it actually returns ALL my products.

I'm using version 1.6.2.0, and I've cleared my cache.

Does anyone have a reason why this isn't working?

EDIT

Calling echo (string) $products->getSelect(); returns

SELECT `e`.*, IF(at_visibility.value_id > 0, at_visibility.value, at_visibility_default.value) AS `visibility`, `cat_index`.`position` AS `cat_index_position`, IF(at_name.value_id > 0, at_name.value, at_name_default.value) AS `name` FROM `catalog_product_entity` AS `e` INNER JOIN `catalog_product_entity_int` AS `at_visibility_default` ON (`at_visibility_default`.`entity_id` = `e`.`entity_id`) AND (`at_visibility_default`.`attribute_id` = '95') AND `at_visibility_default`.`store_id` = 0 LEFT JOIN `catalog_product_entity_int` AS `at_visibility` ON (`at_visibility`.`entity_id` = `e`.`entity_id`) AND (`at_visibility`.`attribute_id` = '95') AND (`at_visibility`.`store_id` = 1) INNER JOIN `catalog_category_product_index` AS `cat_index` ON cat_index.product_id=e.entity_id AND cat_index.store_id=1 AND cat_index.category_id='13' AND cat_index.is_parent=1 LEFT JOIN `catalog_product_entity_varchar` AS `at_name_default` ON (`at_name_default`.`entity_id` = `e`.`entity_id`) AND (`at_name_default`.`attribute_id` = '65') AND `at_name_default`.`store_id` = 0 LEFT JOIN `catalog_product_entity_varchar` AS `at_name` ON (`at_name`.`entity_id` = `e`.`entity_id`) AND (`at_name`.`attribute_id` = '65') AND (`at_name`.`store_id` = 1) WHERE (((IF(at_visibility.value_id > 0, at_visibility.value, at_visibility_default.value) = '4') OR (IF(at_visibility.value_id > 0, at_visibility.value, at_visibility_default.value) = '2'))) ORDER BY `name` as

Upvotes: 2

Views: 3761

Answers (1)

wierdo
wierdo

Reputation: 426

It works for me when simplified to:

$cat = Mage::getModel('catalog/product')->setId(198);

$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addCategoryFilter($cat)
    ->addAttributeToSelect("*")
    ->setOrder('name','asc')
    ->load();

foreach($products as $p) {
    print_r($p->getSku().PHP_EOL);
}

By the way, SQL is more readable (to me, anyway) if you put the addAttributeToSelect call immediately after getCollection.

Upvotes: 5

Related Questions