custom/plugins/ZeobvVisibleDiscounts/src/Storefront/Subscriber/SalesChannelSubscriber.php line 46

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Zeobv\VisibleDiscounts\Storefront\Subscriber;
  4. use Shopware\Core\Framework\Struct\ArrayEntity;
  5. use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityLoadedEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Zeobv\VisibleDiscounts\Service\ConfigService;
  8. use Zeobv\VisibleDiscounts\Service\PromotionService;
  9. /**
  10.  * Class SalesChannelSubscriber
  11.  *
  12.  * @package Zeobv\VisibleDiscounts\Storefront\Subscriber
  13.  */
  14. class SalesChannelSubscriber implements EventSubscriberInterface
  15. {
  16.     protected PromotionService $promotionService;
  17.     protected ConfigService $configService;
  18.     /**
  19.      * SalesChannelSubscriber constructor.
  20.      */
  21.     public function __construct(
  22.         PromotionService $promotionService,
  23.         ConfigService    $configService
  24.     )
  25.     {
  26.         $this->promotionService $promotionService;
  27.         $this->configService $configService;
  28.     }
  29.     /**
  30.      * @return array<string>
  31.      */
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return ['sales_channel.product.loaded' => 'loaded'];
  35.     }
  36.     /**
  37.      * @throws \Exception
  38.      */
  39.     public function loaded(SalesChannelEntityLoadedEvent $event): void
  40.     {
  41.         $salesChannelContext $event->getSalesChannelContext();
  42.         $activateVisibleDiscounts $this->configService->getActivateVisibleDiscounts($salesChannelContext->getSalesChannel());
  43.         if ($activateVisibleDiscounts== false) {
  44.             return;
  45.         }
  46.         # Event can get fired multiple times, return to avoid applying discounts multiple times
  47.         if ($salesChannelContext->hasExtension('zeobvDiscountsApplied')) {
  48.             return;
  49.         }
  50.         $salesChannelContext->addExtension('zeobvDiscountsApplied', new ArrayEntity(['applied' => true]));
  51.         $this->promotionService->overrideCalculatedListPrices($event->getEntities(), $salesChannelContext);
  52.         # Clean up
  53.         $salesChannelContext->removeExtension('zeobvDiscountsApplied');
  54.     }
  55. }