to filter products by certain category following code sample is used


$collection = $this->productCollectionFactory->create();

return $collection;

This does not return products from sub categories even though all categories are marked "Is Anchor" => "Yes".

How can I retrieve products from given category and it's sub-categories based on "Is Anchor" functionality?


Figured it out. Turns out there is a method getAllChildren() that returns category ID along all sub-category IDs.

So to wrap it up:

$categoriesFilter = [
    'eq' => $category->getAllChildren(true)
  • Where do you get the $collection variable from? – James Harrington Apr 10 '18 at 14:38
  • This works well but for completeness, and for other people reading this answer the $category object needs to be instantiated from the CategoryFactory to load your required category ID in order to pass the child array to the filter. See here for more info on loading $category magento.stackexchange.com/questions/96177/… – paj Oct 19 '19 at 16:04

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.