I'm having issue with the pagination and the layered navigation on filtered product collection. The goal is to filter the products by a customer attribute and show only products related to that attribute. The filter is fine except that I get more pages than really available. By example, a certain filter would return 10 products (which is correct) but give me 3 pages of 12 products (page 2 and page 3 doesn't show anything but the pager still show 3 pages). Same thing for the layered navigation on the left. I got results from not available products. By example, the color filter shows 2 products matching this color, but once clicked, no product is listed (it is normal, there is no blue product for that filter) but the results are still showing.
Based on that post
di.xml :
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Model\ResourceModel\Product\Collection">
<plugin name="sidebarFilters" type="Vendor\Module\Model\Plugin\SidebarFilter"/>
</type>
</config>
Sidebarfilter.php:
<?php namespace Vendor\Module\Model\Plugin;
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
class SidebarFilter
{
/**
* aroundAddFieldToFilter method
*
* @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection
* @param \Closure $proceed
* @param $fields
* @param null $condition
*
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection
*/
public function aroundAddFieldToFilter(ProductCollection $collection, \Closure $proceed, $fields, $condition = null)
{
$collection->addAttributeToFilter('brand',[ 'nin' => array(31, 32, 33) ]);
return $fields ? $proceed($fields, $condition) : $collection;
}
}
Everything is fine except pagination and layered navigation.
Thanks for help !