4

I'm creating an extension that filters the product collection. The filter works perfect, but it looks like pagination is not updated. I see 6 pages, but on page 2 the page is already empty.

Here's my code:

Register the plugin:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <type name="Magento\Catalog\Model\ResourceModel\Product\Collection">
        <plugin name="sidebarFilters" type="Silvan\Sidebar\Model\Plugin\SidebarFilter"/>
    </type>
</config>

The plugin:

public function aroundAddFieldToFilter(ProductCollection $collection, \Closure $proceed, $fields, $condition = null)
{
    if ( $fields === 'category_ids' )
    {
        // Get brand
        $brands = isset($_GET[ 'brands' ]) ? $_GET[ 'brands' ] : false;

        if ( $brands )
        {
            $brands = array_map(function($brand) {
                return urldecode($brand);
            }, $brands);

            $collection->addAttributeToFilter('brand', ['in' => $brands]);
        }
    }

    /** Do not try to pass empty $fields to addFieldToFilter, it will cause exception */
    return $fields ? $proceed($fields, $condition) : $collection;
}

Does anyone have a clue?

  • just a curiosity. I know it sounds strange, but bear with me. If you disable the Magento_CatalogSearch module, does it work? – Marius Mar 3 '16 at 14:48
  • When I disable Magento_CatalogSearch and Magento_Search my plugin stops working. Breakpoint on the first line of the function aroundAddFieldToFilter is not called anymore. – Silvan Mar 3 '16 at 15:17
  • Never mind then. Sorry for bothering. I had something similar a while ago and it worked only when the catalogsearch module was disabled. – Marius Mar 3 '16 at 15:18
  • Do you mark brand as filterable attribute in admin panel? – KAndy Mar 6 '16 at 17:53
  • Do you mean the option 'Use in Filter Options'? This option is enabled, yes. – Silvan Mar 7 '16 at 8:11
1

You need use public addFieldToFilter to pass parameters to fulltext search. Try write you code like:

public function aroundAddFieldToFilter(ProductCollection $collection, \Closure $proceed, $fields, $condition = null)
{
    if ( $fields === 'category_ids' )
    {
        // Get brand
        $brands = isset($_GET[ 'brands' ]) ? $_GET[ 'brands' ] : false;

        if ( $brands )
        {
            $brands = array_map(function($brand) {
                return urldecode($brand);
            }, $brands);

            // $collection->addAttributeToFilter('brand', ['in' => $brands]);
            $proceed('brand', $brands);
        }
    }

    /** Do not try to pass empty $fields to addFieldToFilter, it will cause exception */
    return $fields ? $proceed($fields, $condition) : $collection;
}
  • Thanks for you response. This seems to make sense, but unfortunately it's not working. It does not filter anymore if I use $proceed('brand', $brands);. – Silvan Mar 14 '16 at 8:20
  • can you show result sql query? – KAndy Mar 14 '16 at 10:46

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.