custom/plugins/ProcWegmannTheme/src/Subscriber/ProductListingFeaturesSubscriber.php line 51

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Proclane\WegmannTheme\Subscriber;
  3. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  4. use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
  5. use Shopware\Core\Content\Product\Events\ProductSearchResultEvent;
  6. use Shopware\Core\Content\Product\Events\ProductSuggestCriteriaEvent;
  7. use Shopware\Core\Content\Product\SalesChannel\Exception\ProductSortingNotFoundException;
  8. use Shopware\Core\Content\Product\SalesChannel\Listing\FilterCollection;
  9. use Shopware\Core\Content\Product\SalesChannel\Sorting\ProductSortingCollection;
  10. use Shopware\Core\Content\Product\SalesChannel\Sorting\ProductSortingEntity;
  11. use Shopware\Core\Framework\Context;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\EntityAggregation;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  16. use Shopware\Core\System\SystemConfig\SystemConfigService;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\HttpFoundation\Request;
  19. class ProductListingFeaturesSubscriber implements EventSubscriberInterface
  20. {
  21.     
  22.     private SystemConfigService $systemConfigService;
  23.     private  $salesChannelProductRepository;
  24.     public function __construct(
  25.         SystemConfigService $systemConfigService,
  26.          $salesChannelProductRepository)
  27.     {
  28.         $this->systemConfigService $systemConfigService;
  29.         $this->salesChannelProductRepository $salesChannelProductRepository;
  30.     }
  31.     public static function getSubscribedEvents() 
  32.     {
  33.         return [
  34.             ProductSuggestCriteriaEvent::class => [
  35.                 ['handleSearchSuggestEvent'200]
  36.             ],
  37.             ProductSearchCriteriaEvent::class => [
  38.                 ['handleSearchSuggestEvent'200]
  39.             ]
  40.         ];
  41.     }
  42.     public function handleSearchSuggestEvent(ProductSuggestCriteriaEvent|ProductSearchCriteriaEvent $event): void
  43.     {   
  44.         $criteria $event->getCriteria();
  45.         $request $event->getRequest();
  46.         $salesChannelContext $event->getSalesChannelContext();
  47.         $cloneCriteria = clone $criteria;
  48.         $term $request->get('search');
  49.         $cloneCriteria->setTerm(null);
  50.         $cloneCriteria->addFilter(new EqualsFilter('productNumber'$term));
  51.         
  52.         //in base al term fare regex
  53.         $pattern '/^(?=.*\d)(?=.*-).+$/';
  54.         $exactMatch false;
  55.         if (preg_match($pattern$term)) {
  56.             $ids $this->salesChannelProductRepository->searchIds($cloneCriteria$salesChannelContext);
  57.             //se count < 0 allora assegno al criteria
  58.             if($ids->getTotal() > 0) {
  59.                 $criteria->setTerm(null);
  60.                 $criteria->addFilter(new EqualsFilter('productNumber'$term));
  61.                 $exactMatch true;
  62.             }
  63.         } 
  64.         $criteria->assign(['exactMatch' => $exactMatch]);
  65.     }
  66. }