0

How to filter product collection using more than two category. In magento1, we can use ->addAttributeToFilter('category_ids',array('finset'=>'36,37')); but how can i do this in magento2? Also i have checked Multiple category filter extension. Collection show correct but other filters (Like Price,color) and collection size and pagination give a wrong data.

1

Add this categories filter

$catalog_ids = array('10','20');

$collection->addCategoriesFilter(array('in' => $catalog_ids));

Or use below full product collection with categories filter

public function __construct( \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory ) { $this->collectionFactory = $collectionFactory; }

$catalog_ids = array('10','20');

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

$collection->addAttributeToSelect('*');

$collection->addCategoriesFilter(array('in' => $catalog_ids));

I hope this might help you.

Thanks

  • Same code i used but pagination and total product and price filter show wrong data. – nil108 Nov 16 '17 at 11:36

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.