custom/plugins/CoeProductNumberSearchSw6/src/Subscriber/SuggestPageSubscriber.php line 30

Open in your IDE?
  1. <?php
  2. namespace CoeProductNumberSearchSw6\Subscriber;
  3. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  4. use Shopware\Core\Defaults;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  6. use Shopware\Core\Framework\Feature;
  7. use Shopware\Core\System\Snippet\Files\AbstractSnippetFile;
  8. use Shopware\Storefront\Page\Suggest\SuggestPageLoadedEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. /**
  11.  * Class SuggestPageSubscriber
  12.  * @package CoeProductNumberSearchSw6\Subscriber
  13.  * @author Jeffry Block <jeffry.block@codeenterprise.de>
  14.  */
  15. class SuggestPageSubscriber implements EventSubscriberInterface {
  16.     public static function getSubscribedEvents() {
  17.         return [
  18.             SuggestPageLoadedEvent::class => "onSuggestPageLoaded"
  19.         ];
  20.     }
  21.     /**
  22.      * @param SuggestPageLoadedEvent $event
  23.      * @author Jeffry Block <jeffry.block@codeenterprise.de>
  24.      */
  25.     public function onSuggestPageLoaded(SuggestPageLoadedEvent $event) :void{
  26.         /** @var string $term */
  27.         $term $event->getPage()->getSearchTerm();
  28.         /** @var EntitySearchResult $result */
  29.         $result $event->getPage()->getSearchResult();
  30.         #Check if the product number of the first result matches the search term
  31.         if($result->count() == 0){
  32.             return;
  33.         }
  34.         /** @var SalesChannelProductEntity $firstProduct */
  35.         $firstProduct $result->first();
  36.         if($firstProduct === null){
  37.             return;
  38.         }
  39.         $res $result->filter(function($product) use($term){
  40.             /** @var string lowercased term $t */
  41.             $t strtolower(preg_replace("/\.(.*)/i"""$term));
  42.             /** @var string productnumber of first found product $p */
  43.             $p strtolower(preg_replace("/\.(.*)/i"""$product->getProductNumber()));
  44.             return $p === $t;
  45.         });
  46.         if($res->count() > 0){
  47.             /*
  48.              * Since we can't use $res->setTotal(), we simply create a new
  49.              * result based on the original result but change the total.
  50.              */
  51.             if ($res->getTotal() != && !Feature::isActive('v6.5.0.0')) {
  52.                 $res = new EntitySearchResult(
  53.                     $res->getEntity(),
  54.                     $res->getEntities()->count(), // overwrite the count
  55.                     $res->getEntities(),
  56.                     $res->getAggregations(),
  57.                     $res->getCriteria(),
  58.                     $res->getContext()
  59.                 );
  60.             }
  61.             $event->getPage()->setSearchResult($res);
  62.         }
  63.     }
  64. }