3

I want to get specific category product collection, not include it's child category product in it .

Below code which i have tried.

$category = Mage::getModel('catalog/category')->load(5);
 $getCollections = $category->getProductCollection()
         ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
         ->addAttributeToFilter('visibility', 4)
         ->addAttributeToSelect('*');

can any one please help me ?

2 Answers 2

7

Try this:

$category = Mage::getModel('catalog/category')->load(5);
$isAnchorFlag = $category->getIsAnchor();
$category->setIsAnchor(false);
$getCollections = $category->getProductCollection()
         ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
         ->addAttributeToFilter('visibility', 4)
         ->addAttributeToSelect('*');
$category->setIsAnchor($isAnchorFlag);
1
  • @ Marius : thanks , your above code is helpful to me. Commented Jun 26, 2017 at 6:48
0

I'll do this with a little twist with your code.

$category = Mage::getModel('catalog/category')->load(5);
/* Load all child categories  */
$child_cats = $category->getChildren();
$getCollections = $category->getProductCollection()
     ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
            ->addAttributeToFilter('visibility', 4)
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('entity_id', array('neq' => $child_cats));

Ref: https://stackoverflow.com/questions/15600354/product-collection-exclude-products-from-a-certain-category https://stackoverflow.com/questions/18525419/get-child-categories-magento

3
  • 1
    I think you misunderstood the question. Your answer won't work. It will return strange results. You are getting a list of products and excluding some of them that have the ids the same as the child categories of the main category.
    – Marius
    Commented Jun 26, 2017 at 6:09
  • I think it's strange to include a product within a category AND it's child category at the same time. Just curious does it happen in reality?
    – PY Yick
    Commented Jun 26, 2017 at 6:15
  • You don't need to include the product in a child category also. It is done automatically if the category is marked as is_anchor.
    – Marius
    Commented Jun 26, 2017 at 6:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.