I am currently using the following code to return the products in category with id bulbsCategoryId (I get bulbsCategoryId from configuration)

$collection = $this->_productCollectionFactory->create();
$bulbsCategory = $this->_categoryFactory->create()->load($bulbsCategoryId);

This works fine if bulbsCategory is part of the store categories hierarchy. However I would like to use a category that is outside the store categories hierarchy (has a different roo category). Suppose I have the following categories:

Root (this is the store root category)
  Indoors lights
  Outdoors lights
  Light bulbs
  Common light bulbs (I want to filter by this category)

All products in Common light bulbs are also in Light bulbs but not the other way around. In this case the above code returns no results.

Is there a way to achieve this in magento 2?

Or do I have to move Common light bulbs under Root and hide it?

(I am using magento 2.3.1 - no need for backward compatibility)

Your Answer

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

Browse other questions tagged or ask your own question.