0

I am using Magento ver. 2.3.6.

I want to filter product collection by custom product attribute and I am using below code:-

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\Block\Product\ProductList\Toolbar">
        <plugin name="Vendor_CustomModule::customCollection" type="Vendor\CustomModule\Plugin\Product\ProductList\Toolbar"/>
    </type>
</config>

Toolbar.php

<?php

namespace Vendor\CustomModule\Plugin\Product\ProductList;

class Toolbar
{
    
    public function aroundSetCollection(\Magento\Catalog\Block\Product\ProductList\Toolbar $subject, \Closure $proceed, $collection) 
    {
        $result       = $proceed($collection);
        $currentOrder = $subject->getCurrentOrder();
        $dir          = $subject->getCurrentDirection();        
        
        $subject->getCollection()->addAttributeToFilter('country_of_manufacture', ['eq' => 'IN']);          
        return $result;
    }   
}

Above code filter the product collection only on category page, I want this filter result in whole website, including, new product, featured product, bestseller products, realted products, upsell products, crosssell products and search result.

Please let me know where I add my custom filter in product collection, So filter result will show on whole website products.

0

Not sure if you can achieve this from one singe place.
But here is how I did it. I hope I covered all the places.

First, I create my own class / method that applies an extra filter to a product collection.

<?php
declare(strict_types=1);

namespace [Vendor]\[Module]\Model;

use Magento\Catalog\Model\ResourceModel\Product\Collection;

class RestrictionApplier
{
    /**
     * @param Collection $collection
     */
    public function apply(Collection $collection)
    {
        $collection->addAttributeToFilter( 'country_of_manufacture', ['eq' => 'IN']);
    }
}

then I used this class / method in after plugins for the following methods:

  • Magento\Catalog\Model\Product\Link::getProductCollection
  • Magento\CatalogWidget\Block\Product\ProductsList::createCollection

and in an observer on the event catalog_block_product_list_collection

The plugins look like this:

<?php
declare(strict_types=1);

namespace [Vendor]\[Module]\Plugin\Catalog\Model\Product;

use [Vendor]\[Module]\Model\RestrictionApplier;
use Magento\Catalog\Model\Product\Link;
use Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection;

class LinkPlugin
{
    /**
     * @var RestrictionApplier
     */
    private $restrictionApplier;
   
    /**
     * ProductListPlugin constructor.
     * @param RestrictionApplier $restrictionApplier
     */
    public function __construct(RestrictionApplier $restrictionApplier)
    {
        $this->restrictionApplier = $restrictionApplier;
    }

    /**
     * @param Link $subject
     * @param Collection $collection
     * @return Collection
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function afterGetProductCollection(Link $subject, Collection $collection)
    {
        $this->restrictionApplier->apply($collection);
        return $collection;
    }
}

for the observer is similar

public function execute(Observer $observer)
{
    /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
    $collection = $observer->getCollection();
    $this->restrictionApplier->apply($collection);
}
4
  • No result noticed after add this code in my custom plugin, Please can you simplify this answer and I am checking again if I miss anything in my code. Nov 3 '20 at 12:44
  • you can check if your plugin gets called. Put a log in it or something
    – Marius
    Nov 3 '20 at 16:44
  • Yes plugin is not working. I using this code in etc/frontend/di.xml prntscr.com/vd9az4 please let me know if any issue in this Nov 4 '20 at 12:00
  • Any comments on my plugin call issue? Nov 6 '20 at 7: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.