Magento – Having pagination issue with collection with addAttributeToFilter

collection;magento2product-collection

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 !

Best Answer

I, too, am having the very same issue - filtering works perfectly fine, but no matter where I perform the filtering, the LayeredNavigation will not work correctly.

Whilst I can't "comment", hopefully this "partial answer" can help you along?

By moving the filtering into a CollectionFilter, pagination works fine for me.

My di.xml:

<preference for="Magento\Catalog\Model\Layer\Category\CollectionFilter" type="Company\Plugin\Model\Layer\Category\CollectionFilter"/>

The CollectionFilter:

public function filter($collection, \Magento\Catalog\Model\Category $category) {
        $collection
            ->addAttributeToSelect($this->catalogConfig->getProductAttributes())
            ->addMinimalPrice()
            ->addFinalPrice()
            ->addTaxPercents()
            ->addUrlRewrite($category->getId())
            ->setVisibility($this->productVisibility->getVisibleInCatalogIds());

            $filterBy = {custom code in here}

            $collection->addAttributeToFilter('supplier_code', array('IN' => $filterBy));

    }

I am just not sure if the LayeredNavigation is working correctly, or filtering must be performed earlier in the execution chain.

Can anyone else shed some light on this?