6

In my store parent category load collection of its child category also.
I can solve it by Is Anchor to "No".
But by doing this Filter is not coming in the left bar so it is not the solution of my problem.
How can I load the products which I assigned to the category itself?

2
+25

I solved this for Magento 1. You can see the answer in here.
You can try a similar approach in M2. Not sure it will work, though.
The anchor flag is taken into consideration in the Magento\Catalog\Model\ResourceModel\Product\Collection::addCategoryFilter() method.

Maybe it will work with an around plugin, where you set the anchor to 0, then execute the initial method and set it back to the value it was before.

Something like this (untested):

public function aroundAddCategoryFilter(
   \Magento\Catalog\Model\ResourceModel\Product\Collection $subject,
   callable $proceed,
   \Magento\Catalog\Model\Category $category
) {
    //remember the category anchor flag
    $anchor = $category->getIsAnchor(); 
    //set anchor to 0.
    $category->setIsAnchor(0);
    //execute the original method and remember the result
    $result = $proceed($category);
    //set back the anchor flag on the category
    $category->setIsAnchor($anchor);
    //return what the original method returned;
    return $result;
}

More info about creating a plugin in the official documentation

  • It filter the products but not made changes in left side filter.It show all attributes of child category's products too,and wrong product count including child category. – Ketan Borada Aug 11 '18 at 6:20
  • I have achieved filter products by changing _applyProductLimitations() function. Do you have any idea for Filter?.Anyway thanks for help it better than my solution – Ketan Borada Aug 11 '18 at 6:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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