I am retrieving a product collection and filtering the list by category. I have just had to add some sub categories to one of them and now the products in the sub-categories are appearing in the collection for the parent category and I cannot work out how to only show products directly assigned to the parent category:
Here is an example of the code:
$cat = Mage::getModel("[custom-namepace]/Categories"); //this just extends the native Categories model $cat->load($catId); //some number $products = Mage::getResourceModel("catalog/product_collection") ->setStoreId(Mage::app()->getStore()->getId()) ->addUrlRewrite() ->addCategoryFilter($cat) ->setOrder("name","asc");
I am unable to add a filter on category_id as that causes an error. I have tried:
->addAttributeToFilter("category_id",array("in"=>$cat->getId() ) )
These all throw a fatal error like: Fatal error: Call to a member function getBackend() on a non-object in [...]\app\code\core\Mage\Eav\Model\Entity\Abstract.php on line 816
Any pointers on how to avoid picking up products in child categories?
I have now also tried to get the products list through the category and I still get products from the child categories.
$products = $cat->getProductCollection() ->addAttributeToSelect("name") ->addUrlRewrite() ->setOrder("name","asc");