1 Comment

Magento2 – addAttributeToFilter wrong pagination

Magento2: How to generate admin url with secure key from a controller
How to get customer comment in admin custom field?

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="MagentoCatalogModelResourceModelProductCollection">
        <plugin name="sidebarFilters" type="SilvanSidebarModelPluginSidebarFilter"/>
    </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?

Related Posts

  • Magento2 price filter with configurable products (2)
    I created a custom price filter (without layered navigation). This function works great on simple products, but configurable products are filtered (maybe because they don't have a price?). This is my function: <?php namespace SilvanSidebarModelPlugin; use […]
  • Customer grid, custom field – filter on a range (0)
    I have a customer grid with an extra field - customer Total Orders, or order_count below. This displays fine initially but if you want to filter on it it doesn't work. It doesn't give a fatal error, nor is there anything in var/log/ but just doesn't work. protected function […]
  • Copy product description to specific storeview with an extra filter (0)
    i have this script for copying the description of 2 storeviews to a 3th storeview. <?php require_once 'abstract.php'; class Mage_Shell_DescMerge extends Mage_Shell_Abstract { const STORE_ID_1 = 1; // Replace this with correct value const STORE_ID_2 = 2; // Replace this […]
  • magento 1.9.2 – apply layer filter to custom product collection (1)
    I want to apply layer navigation filter in custom product collection. Ex:- http://domain.com/seller/collection/test/?condition=new&brand=xyz Now I have collection of products of particular seller as per below code. $layer = Mage::getModel('catalog/layer'); $category = […]
  • Orders Collection Magento 2 (1)
    my objective is to find all orders with status "pending". Seems the way to do this is to use the OrderCollection class Modelx extends MagentoFrameworkAppHelperAbstractHelper { /** * @var MagentoSalesModelResourceModelOrderCollectionFactory */ protected […]
  • Orders Collection Magento 2 (1)
    my objective is to find all orders with status "pending". Seems the way to do this is to use the OrderCollection class Modelx extends MagentoFrameworkAppHelperAbstractHelper { /** * @var MagentoSalesModelResourceModelOrderCollectionFactory */ protected […]
  • Magento 2 layered navigation custom collection (0)
    I want to implement layered navigation on my custom controller action where I have listed all the products of a particular brand. I have called the layered navigation block on this page but the layered navigation filters are not populated by the product collection attributes. Please […]
  • How to get Featured product collection filter by current category Magento2? (0)
    I have created a featured product extension for a Magento 2 store. It's a simple extension. I have added is_featured yes/no button in admin product section. If it's enabled that product is show in a featured section. It's working perfect. Now, I want to add current category filter […]
  • Multiselect categories in layered navigation (0)
    One of catalogs consists of 3 levels of which the fourth is a differentiation in brands. Consider the following example as level 2 and 3 Hatchbacks Volkswagen Volvo Seat Audi Basically we want to show all products when a customer is browsing the hatchbacks. We made the […]
  • Magento pagination is not working after removing some products from a collection (2)
    In the category products page I need to display the products in order which we select from the option drop down (price, position or name). At the same time I need to move Out Of Stock products last. I override the Mage_Catalog_Block_Product_List_Toolbar setCollection() function and […]
Magento2: How to generate admin url with secure key from a controller
How to get customer comment in admin custom field?

1 Comment on this article

  • User November 12, 2016

    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;
    }
    

    Reply

Add a comment

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax