I am working on a Magento 2.1.7 shop and I have created a child-theme of Magento Blank

The products are organized into categories and subcategories. In other words, they are organized into parent categories and children categories.

Whenever a product is assigned, through the Magento admin, to a child category, I want it to be automatically assigned to all the ancestor categories of that child category.

The Footwear category tree

I want to manually assign all the boot brands in the store to the "Boots" child category add I want all boots to be automatically put, by Magento, in the "Footwear" parent category.

Question: What is the programming solution that would do the job, recursively?

  • did you find a code solution to this? I have the saem issue and I'd like to assign parent categories to products programmatically
    – zekia
    May 23 '19 at 13:16

There is a simple solution which Magento 2.x provides by default.

  • Go to Products > Categories.
  • Click on the (parent) category in which you want to show the products of the sub-category.
  • Under Display Settings, set Yes for Anchor field.
  • Save the category, flush Magento's cache and reindex.

Please let me know if the solution works for you.

  • How do I reindex? Jan 9 '18 at 13:54
  • in the root of your Magento instance run php bin/magento indexer:reindex but I would run the following command to reindex and clear cache: php bin/magento indexer:reindex && bin/magento cache:flush
    – Vlad Patru
    Jan 9 '18 at 14:04
  • It does work, in principle, but in practice, there is a problem. I have: Home > Intretinere Exterior > Spalare & Detailing Rapid > Spuma Prespalare & Sampon Auto (the site's bradcrumbs). Intretinere Exterior shows products, but NOT Spalare & Detailing Rapid. Jan 9 '18 at 14:34
  • @Mohit Kumar Arora: It does work, but I'd rather not do repetitive operations from the admin, by hand. I am looking for a code solution. Thanks a lot for the effort and the very useful tip :) Jan 9 '18 at 14:36
  • @RazvanZamfir, why have you unaccepted the solution?? Jan 19 '18 at 10:26

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.