custom/static-plugins/AbasConnector/src/Subscribers/Product/ProductSubscriber.php line 96

Open in your IDE?
  1. <?php
  2. namespace Abas\AbasConnector\Subscribers\Product;
  3. use Abas\AbasConnector\Service\Customer\AbasCustomerItemNumberService;
  4. use Abas\AbasConnector\Service\Utils\Config\PluginConfigReader;
  5. use RuntimeException;
  6. use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
  7. use Shopware\Core\Checkout\Customer\Event\CustomerLogoutEvent;
  8. use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
  9. use Shopware\Core\Content\Product\Events\ProductSuggestCriteriaEvent;
  10. use Shopware\Core\Content\Product\ProductEvents;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Query\ScoreQuery;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\HttpFoundation\Session\Session;
  19. class ProductSubscriber implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * @var Session
  23.      */
  24.     private Session $session;
  25.     /**
  26.      * @var AbasCustomerItemNumberService
  27.      */
  28.     private AbasCustomerItemNumberService $abasCustomerItemNumberService;
  29.     /**
  30.      * @var bool
  31.      */
  32.     private bool $isCustomerItemNumbersActive;
  33.     /**
  34.      * ProductSubscriber constructor.
  35.      *
  36.      * @param AbasCustomerItemNumberService $abasCustomerItemNumberService
  37.      * @param Session $session
  38.      * @param PluginConfigReader $pluginConfigReader
  39.      * @throws \Exception
  40.      */
  41.     public function __construct(
  42.         AbasCustomerItemNumberService $abasCustomerItemNumberService,
  43.         Session $session,
  44.         PluginConfigReader $pluginConfigReader
  45.     ) {
  46.         $this->abasCustomerItemNumberService $abasCustomerItemNumberService;
  47.         $this->session $session;
  48.         $this->isCustomerItemNumbersActive $pluginConfigReader->getIsCustomerItemNumberActive();
  49.     }
  50.     /**
  51.      * @inheritDoc
  52.      */
  53.     public static function getSubscribedEvents()
  54.     {
  55.         return [
  56.             CustomerLoginEvent::class => 'storeCustomerToSession',
  57.             CustomerLogoutEvent::class => 'removeCustomerToSession',
  58.             ProductEvents::PRODUCT_LOADED_EVENT => 'onProductsLoaded',
  59.             ProductSearchCriteriaEvent::class => 'addSearchCriteria',
  60.             ProductSuggestCriteriaEvent::class => 'addSuggestCriteria'
  61.         ];
  62.     }
  63.     /**
  64.      * @param CustomerLoginEvent $event
  65.      *
  66.      * @return void
  67.      */
  68.     public function storeCustomerToSession(CustomerLoginEvent $event)
  69.     {
  70.         $this->session->set("customer"$event->getCustomer()->getCustomerNumber());
  71.     }
  72.     /**
  73.      * @param CustomerLogoutEvent $event
  74.      *
  75.      * @return void
  76.      */
  77.     public function removeCustomerToSession(CustomerLogoutEvent $event)
  78.     {
  79.         $this->session->remove("customer");
  80.     }
  81.     /**
  82.      * @param EntityLoadedEvent $event
  83.      *
  84.      * @return void
  85.      */
  86.     public function onProductsLoaded(EntityLoadedEvent $event)
  87.     {
  88.         $customer $this->session->get("customer");
  89.         if ($customer && $this->isCustomerItemNumbersActive) {
  90.             $this->abasCustomerItemNumberService->loadCustomerItemNumber($event->getEntities(), $customer);
  91.         }
  92.     }
  93.     /**
  94.      * @param ProductSearchCriteriaEvent $event
  95.      *
  96.      * @return void
  97.      * @throws RuntimeException
  98.      */
  99.     public function addSearchCriteria(ProductSearchCriteriaEvent $event): void
  100.     {
  101.         $customer $event->getSalesChannelContext()->getCustomer();
  102.         if ($customer !== null && $this->isCustomerItemNumbersActive) {
  103.             $request $event->getRequest();
  104.             $customerProductList $this->abasCustomerItemNumberService->loadCustomerItemsByOwnNumber(
  105.                 $request->get("search"),
  106.                 $customer->getCustomerNumber(),
  107.                 10
  108.             );
  109.             if (count($customerProductList) > 0) {
  110.                 $criteria $event->getCriteria();
  111.                 $this->updateFilters($criteria$customerProductList);
  112.                 $this->updateQueries($request->getQueryString(), $criteria$customerProductList);
  113.             }
  114.         }
  115.     }
  116.     /**
  117.      * TODO: gleicher Methodenkörper wie addSearchCriteria().
  118.      *
  119.      * @param ProductSuggestCriteriaEvent $event
  120.      *
  121.      * @return void
  122.      * @throws RuntimeException
  123.      */
  124.     public function addSuggestCriteria(ProductSuggestCriteriaEvent $event): void
  125.     {
  126.         $customer $event->getSalesChannelContext()->getCustomer();
  127.         if ($customer !== null && $this->isCustomerItemNumbersActive) {
  128.             $request $event->getRequest();
  129.             $customerProductList $this->abasCustomerItemNumberService->loadCustomerItemsByOwnNumber(
  130.                 $request->get("search"),
  131.                 $customer->getCustomerNumber(),
  132.                 10
  133.             );
  134.             if (count($customerProductList) > 0) {
  135.                 $criteria $event->getCriteria();
  136.                 $this->updateFilters($criteria$customerProductList);
  137.                 $this->updateQueries($request->getQueryString(), $criteria$customerProductList);
  138.             }
  139.         }
  140.     }
  141.     /**
  142.      * @param Criteria $criteria
  143.      * @param array $customerProductList
  144.      *
  145.      * @return void
  146.      */
  147.     private function updateFilters(Criteria $criteria, array $customerProductList): void
  148.     {
  149.         $newFilters null;
  150.         $filters $criteria->getFilters();
  151.         foreach ($filters as $filter) {
  152.             if ($filter instanceof EqualsAnyFilter && $filter->getField() === "product.searchKeywords.keyword") {
  153.                 $values $filter->getValue();
  154.                 foreach ($customerProductList as $customerProduct) {
  155.                     $values[] = $customerProduct->getProduct();
  156.                 }
  157.                 $newFilters[] = new EqualsAnyFilter("product.searchKeywords.keyword"$values);
  158.             } else {
  159.                 $newFilters[] = $filter;
  160.             }
  161.         }
  162.         if ($newFilters) {
  163.             $criteria->resetFilters();
  164.             foreach ($newFilters as $filter) {
  165.                 $criteria->addFilter($filter);
  166.             }
  167.         }
  168.     }
  169.     /**
  170.      * @param string $origQuery
  171.      * @param Criteria $criteria
  172.      * @param array $customerProductList
  173.      *
  174.      * @return void
  175.      */
  176.     private function updateQueries(string $origQueryCriteria $criteria, array $customerProductList)
  177.     {
  178.         $newQueries null;
  179.         $origQuery explode("="$origQuery)[1];
  180.         $queries $criteria->getQueries();
  181.         foreach ($queries as $query) {
  182.             if ($query instanceof ScoreQuery && $query->getQuery()->getField() === "product.searchKeywords.keyword") {
  183.                 $values[] = $origQuery;
  184.                 $score $query->getScore();
  185.                 $scoreField $query->getScoreField();
  186.                 foreach ($customerProductList as $customerProduct) {
  187.                     $newQueries[] = new ScoreQuery(
  188.                         new ContainsFilter(
  189.                             "product.searchKeywords.keyword",
  190.                             $customerProduct->getProduct()
  191.                         ),
  192.                         $score,
  193.                         $scoreField
  194.                     );
  195.                 }
  196.             } else {
  197.                 $newQueries[] = $query;
  198.             }
  199.         }
  200.         if ($newQueries) {
  201.             $criteria->resetQueries();
  202.             foreach ($newQueries as $query) {
  203.                 $criteria->addQuery($query);
  204.             }
  205.         }
  206.     }
  207. }