Back to Top

How to add filter before listing product and search suggestion in Shopware6

Updated 12 November 2021

Introduction

In this topic we discuss about how to be add filter before listing product and search suggestion result in Shopware6. If you want to add filter on product listing , product search suggestion list and product search result event, then please follow this article.

Overview

We can achieve this solution by subscribed event for corresponding result view. like as ProductListingCriteriaEvent for listing product, ProductSuggestCriteriaEvent for product search suggestion and ProductSearchCriteriaEvent for product search result page.

Subscribed Event in Subscriber

First we create subscriber file ProductResultCriteriaSubscriber.php for subscribed multiple event and register it in config.xml file. After creating subscriber file , subscribed ProductListingCriteriaEvent and ProductSuggestCriteriaEvent class with relative functions in getSubscribedEvents function. After creating functions for Event class we can add filter in existing product result criteria. Getting existing criteria through event and add it own filter for desired results.
Like here we filter some desired products ids in current criteria. So get current criteria by $event and add new filter.

How to upload media through URL

class ProductResultCriteriaSubscriber implements EventSubscriberInterface
{
    protected $container;
    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }
    public static function getSubscribedEvents()
    {
        return [
            ProductListingCriteriaEvent::class=> "productListingCriteria",
            ProductSuggestCriteriaEvent::class => "productSuggestCriteria"
            
        ];
    }
    
    public function productSuggestCriteria(ProductSuggestCriteriaEvent $event){
        $session = new Session();
        $productIds = $session->get('productIds');
        if(empty($productIds)){
            $productIds = [];
        }
        
        $event->getCriteria()->addFilter(new EqualsAnyFilter('id',$productIds));
    }
   
    public function productListingCriteria(ProductListingCriteriaEvent $event)
    {
        $session = new Session();
        $productIds = $session->get('productIds');
        if(empty($productIds)){
            $productIds = [];
        }                                                                                                                                                                                                                                        
        $event->getCriteria()->addFilter(new EqualsAnyFilter('id',$productIds));
    }
}

Register Subscriber

After creating subscriber file need to register it config.xml file for global accessing .So we create config.xml file in config folder and register the subscriber.

Start your headless eCommerce
now.
Find out More

<service id=”Webkul\MPHyperlocal\Subscriber\ProductResultCriteriaSubscriber” public=”true”> <tag name=”kernel.event_subscriber” /> <argument type=”service” id=”service_container” /> </service>

Final Summary

Now filter listing all product according to your custom criteria on the storefront.

Thanks for reading, so i hope it will help you. Happy coding 🙂

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home