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 Answer
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.– nil108Nov 16, 2017 at 11:36