Hodgekins
Hodgekins

Reputation: 23

Shopware 6 Include properties in storefront search

I would like the main store search to include product properties.

For example, if I have a product property called "colour", and somebody searches for "red", I would like to include products that have this property item in the search results.

So far I have created a custom plugin that subscribes to the Product Search Criteria Event. here is my event subscriber class:

<?php declare(strict_types=1);

namespace MyPlugin\Subscriber;

use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Content\Product\ProductEvents;
use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;

class CustomSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            ProductEvents::PRODUCT_SEARCH_CRITERIA => 'onProductSearch'
        ];
    }

    public function onProductSearch(ProductSearchCriteriaEvent $event)
    {
      $criteria = $event->getCriteria();
      $criteria->addAssociation('properties');
      dump($event);
    }
}

I can see from the dump() that the association has been added. But the search result doesn't change. I'm not sure where to go from here.

Upvotes: 2

Views: 1271

Answers (1)

mnaczenski
mnaczenski

Reputation: 1626

I have done that before in my plugin: https://github.com/mnaczenski/MNExtendSearch/blob/master/src/Service/MySearchKeywordAnalyser.php#L113

Basically you extend the SearchKeywordAnalyser and add your custom data to the search index.

Upvotes: 3

Related Questions