custom/plugins/TemplaidRecommendations/src/Subscriber/ProductSubscriber.php line 35

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace TemplaidRecommendations\Subscriber;
  3. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  4. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use TemplaidRecommendations\Core\Content\Product\RecommendationDefinition;
  7. use TemplaidRecommendations\Service\RecommendationService;
  8. class ProductSubscriber implements EventSubscriberInterface
  9. {
  10.     private RecommendationService $recommendationService;
  11.     public function __construct(
  12.         RecommendationService $recommendationService
  13.     )
  14.     {
  15.         $this->recommendationService $recommendationService;
  16.     }
  17.     /**
  18.      * @return array|string[]
  19.      */
  20.     public static function getSubscribedEvents()
  21.     {
  22.         return [
  23.             ProductPageLoadedEvent::class => 'onProductPageLoaded'
  24.         ];
  25.     }
  26.     /**
  27.      * @param ProductPageLoadedEvent $event
  28.      */
  29.     public function onProductPageLoaded(ProductPageLoadedEvent $event): void
  30.     {
  31.         if (!$event->getSalesChannelContext()->getCustomer()) {
  32.             return;
  33.         }
  34.         $data = [[
  35.             'productId' => $event->getPage()->getProduct()->getId(),
  36.             'customerId' => $event->getSalesChannelContext()->getCustomerId(),
  37.             'type' => RecommendationDefinition::TYPE_LAST_VIEWED_BY_CUSTOMER
  38.         ]];
  39.         $this->recommendationService->createRecommendations($data$event->getSalesChannelContext()->getContext());
  40.     }
  41. }