custom/plugins/EwGrecoBundle/src/Subscriber/ProductSubscriber.php line 62

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Ew\GrecoBundle\Subscriber;
  3. use Ew\GrecoBundle\Service\BundleService;
  4. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  5. use Shopware\Core\Content\Product\ProductEvents;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class ProductSubscriber implements EventSubscriberInterface
  12. {
  13.     private BundleService $bundleService;
  14.     private EntityRepository $customerDnaProductRepository;
  15.     public function __construct(
  16.         BundleService $bundleService,
  17.         EntityRepository $customerDnaProductRepository
  18.     )
  19.     {
  20.         $this->bundleService $bundleService;
  21.         $this->customerDnaProductRepository $customerDnaProductRepository;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             ProductPageLoadedEvent::class => 'onProductLoaded',
  27.             ProductEvents::PRODUCT_LISTING_RESULT => 'onProductListingLoaded'
  28.         ];
  29.     }
  30.     public function onProductLoaded(ProductPageLoadedEvent $event): void
  31.     {
  32.         try {
  33.             $productId $event->getPage()->getProduct()->getId();
  34.             $customer $event->getSalesChannelContext()->getCustomer();
  35.             $customerId null;
  36.             if ($customer) {
  37.                 $customerId $customer->getId();
  38.             }
  39.             $criteria = new Criteria();
  40.             $criteria->addFilter(new EqualsFilter('productId'$productId));
  41.             $criteria->addFilter(new EqualsFilter('customerId'$customerId));
  42.             $customerDnaProduct $this->customerDnaProductRepository->search($criteria$event->getContext())->getEntities();
  43.             if (count($customerDnaProduct) > 0) {
  44.                 $event->getPage()->getProduct()->setExtensions([
  45.                     'bought' => true,
  46.                 ]);
  47.             }
  48.         } catch (\Exception $exception) {
  49.         }
  50.     }
  51.     public function onProductListingLoaded(ProductListingResultEvent $event): void
  52.     {
  53.         $customer $event->getSalesChannelContext()->getCustomer();
  54.         $customerId null;
  55.         if ($customer) {
  56.             $customerId $customer->getId();
  57.         }
  58.         foreach ($event->getResult()->getEntities()->getElements() as $entity) {
  59.             $productId $entity->getId();
  60.             if ($productId && $customerId) {
  61.                 $checkBoughtDnaProduct $this->bundleService->ifCustomerBoughtProduct($productId$customerId$event->getContext());
  62.                 if ($checkBoughtDnaProduct) {
  63.                     $entity->getExtensions()['foreignKeys']->setExtensions(['bought' => true]);
  64.                     $entity->setExtensions(
  65.                         $entity->getExtensions()
  66.                     );
  67.                 }
  68.             }
  69.         }
  70.     }
  71. }