I have a Magento2 Store (v2.4) where I have multiple locations. for (eg. Jaipur, Delhi, Mumbai etc). I have managed locations using custom attribute using multiple dropdown list and assigned it to product.
I want to filter products on frontend store using this attribute.
These attributes appear in layered navigation and working fine but I want to filter them programmatically.
I created a beforeLoad plugin for \Magento\Catalog\Model\ResourceModel\Product\Collection
using below code
public function beforeLoad(\Magento\Catalog\Model\ResourceModel\Product\Collection $subject, $printQuery = false, $logQuery = false)
{
if (!$subject->isLoaded()) {
$id = 212;
$subject->addAttributeToFilter('store_address', ['finset' => $id]);
}
return [$printQuery, $logQuery];
}
This code filter product collection based on location but toolbar and pagination data is incorrect. For ex there are 3 product available and pagination size is 12 per page so 2 product on page 1 and 1 item appear on page 2.
I want to show all 3 items on page 1.
Please help me how can I implement this filter based on custom attribute.
Thanks