custom/plugins/MadMykey/src/Subscriber/ProductSubscriber.php line 57

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Madco\Mykey\Subscriber;
  4. use Madco\Mykey\Service\MykeyProductService;
  5. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  6. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  7. use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
  8. use Shopware\Core\Content\Product\Events\ProductSearchResultEvent;
  9. use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
  10. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class ProductSubscriber implements EventSubscriberInterface
  13. {
  14.     private MykeyProductService $mykeyProductService;
  15.     public function __construct(MykeyProductService $mykeyProductService)
  16.     {
  17.         $this->mykeyProductService $mykeyProductService;
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             ProductListingCriteriaEvent::class => 'onProductListingCriteria',
  23.             ProductPageCriteriaEvent::class => 'onProductPageCriteria',
  24.             ProductPageLoadedEvent::class => 'onProductPageLoaded',
  25.             ProductListingResultEvent::class => 'onProductListingResult',
  26.             #'sales_channel.product.loaded' => 'salesChannelProductLoaded',
  27.             ProductSearchCriteriaEvent::class => 'onProductListingCriteria',
  28.             ProductSearchResultEvent::class => 'onProductListingResult',
  29.         ];
  30.     }
  31.     public function onProductPageCriteria(ProductPageCriteriaEvent $event): void
  32.     {
  33.         $event->getCriteria()->addAssociation('tags');
  34.     }
  35.     public function onProductListingCriteria(ProductListingCriteriaEvent $event): void
  36.     {
  37.         $event->getCriteria()->addAssociation('tags');
  38.     }
  39.     public function onProductPageLoaded(ProductPageLoadedEvent $event): void
  40.     {
  41.         $product $event->getPage()->getProduct();
  42.         if ($this->mykeyProductService->isMykeyProduct($product)) {
  43.             $this->mykeyProductService->addMykeyExtensionToProduct($product);
  44.         }
  45.     }
  46.     public function onProductListingResult(ProductListingResultEvent $event): void
  47.     {
  48.         foreach ($event->getResult() as $product) {
  49.             if ($this->mykeyProductService->isMykeyProduct($product)) {
  50.                 $this->mykeyProductService->addMykeyExtensionToProduct($product);
  51.             }
  52.         }
  53.     }
  54. }