custom/plugins/ShopWithMeDeal/src/Service/ProductSubscriber.php line 40

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace ShopWithMe\Deal\Service;
  3. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  4. use Shopware\Core\Content\Product\ProductEvents;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  10. use Shopware\Core\System\SystemConfig\SystemConfigService;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class ProductSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var SystemConfigService
  16.      */
  17.     private $systemConfigService;
  18.     private EntityRepositoryInterface $productDealRepository;
  19.     public function __construct(
  20.         SystemConfigService       $systemConfigService,
  21.         EntityRepositoryInterface $productDealRepository,
  22.     )
  23.     {
  24.         $this->systemConfigService $systemConfigService;
  25.         $this->productDealRepository $productDealRepository;
  26.     }
  27.     public static function getSubscribedEvents()
  28.     {
  29.         return [
  30.             ProductEvents::PRODUCT_LISTING_CRITERIA => 'onProductListingCriteria',
  31.             ProductEvents::PRODUCT_LOADED_EVENT => 'onProductLoaded',
  32.         ];
  33.     }
  34.     public function onProductLoaded(EntityLoadedEvent $event): void
  35.     {
  36.         $products $event->getEntities();
  37.         if (count($products) == 1) {
  38.             $productId $products[0]->getId();
  39.             $productDeals $this->productDealRepository->search(
  40.                 (new Criteria())
  41.                     ->addFilter(
  42.                         new EqualsFilter('productId'$productId),
  43.                     )
  44.                     ->addAssociation('languages')
  45.                     ->addSorting(new FieldSorting('startDate'FieldSorting::DESCENDING)),
  46.                 $event->getContext(),
  47.             )->getEntities();
  48.             if ($productDeals->count() > 0) {
  49.                 $products[0]->addExtensions([
  50.                     'deals' => $productDeals,
  51.                     'currentDeal' => $productDeals->getLatestDeal(),
  52.                 ]);
  53.             }
  54.         } else {
  55.             foreach ($products as $product) {
  56.                 if (!empty($product->getExtension('deals'))) {
  57.                     $product->addExtensions([
  58.                         'currentDeal' => $product->getExtension('deals')?->getLatestDeal(),
  59.                     ]);
  60.                 }
  61.             }
  62.         }
  63.     }
  64.     public function onProductListingCriteria(ProductListingCriteriaEvent $event): void
  65.     {
  66.         $criteria $event->getCriteria();
  67.         $criteria->addAssociation('deals');
  68.         $criteria->addAssociation('deals.languages');
  69.     }
  70. }