3

I have a product collection $collection which is created by Magento\Catalog\Model\ResourceModel\Product\Collection

code is:

$collection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$collection->addAttributeToSelect('*');
$collection->addAttributeToFilter('status', array('eq' => \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED));

How can I add multiple category filter in it?

0

1 Answer 1

0

First of all, do not use object manager, create your object from constructor

$protected $collection;

public function __construct(
    ...
    \Magento\Catalog\Model\ResourceModel\Product\Collection $collection
    ...
)
{
    ...
    $this->collection = $collection;
    ...
}

For your category filter use this:

$categories = [12,13,14]; //Category ids in array
$collection->addCategoriesFilter(array('in' => $categories));

Collection will join category table and will add your filter

1
  • If cate id = 13 is children of 12 then it will not return exactly
    – huykon225
    Jun 2, 2021 at 7:22

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.