custom/plugins/ASENextFreeDelivery/src/Subscriber/Frontend.php line 72

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace ASEServices\ASENextFreeDelivery\Subscriber;
  3. use Doctrine\DBAL\DBALException;
  4. use ASEServices\ASENextFreeDelivery\Components\ShippingFreeService;
  5. use Shopware\Core\System\SystemConfig\SystemConfigService;
  6. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  7. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  8. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  9. use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class Frontend implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var array
  15.      */
  16.     protected $config;
  17.     /**
  18.      * @var ShippingFreeService
  19.      */
  20.     private $shippingFreeService;
  21.     public function __construct(
  22.         SystemConfigService $configService,
  23.         ShippingFreeService $shippingFreeService
  24.     ) {
  25.         $this->config              $configService->getDomain('ASENextFreeDelivery');
  26.         $this->shippingFreeService $shippingFreeService;
  27.     }
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             ProductPageLoadedEvent::class       => 'onProductLoaded',
  32.             CheckoutCartPageLoadedEvent::class  => 'onCheckoutPageLoaded',
  33.             OffcanvasCartPageLoadedEvent::class => 'onAjaxCartPageLoaded',
  34.             HeaderPageletLoadedEvent::class     => 'headerLoadedEvent',
  35.         ];
  36.     }
  37.     /**
  38.      * @param HeaderPageletLoadedEvent $event
  39.      *
  40.      * @throws DBALException
  41.      */
  42.     public function headerLoadedEvent(HeaderPageletLoadedEvent $event): void
  43.     {
  44.         $isActivated $this->config['ASENextFreeDelivery.config.subshopActivated'] ?? false;
  45.         $show        $this->config['ASENextFreeDelivery.config.showInHeader'] ?? false;
  46.         if (!$isActivated || !$show) {
  47.             return;
  48.         }
  49.         $event->getPagelet()->setExtensions([
  50.             'aseNextFreeDelivery' => [
  51.                 'difference' => $this->shippingFreeService->calculateShippingCostsDifference(
  52.                     $event->getSalesChannelContext()
  53.                 )
  54.             ]
  55.         ]);
  56.     }
  57.     /**
  58.      * @param OffcanvasCartPageLoadedEvent $event
  59.      *
  60.      * @throws DBALException
  61.      */
  62.     public function onAjaxCartPageLoaded(OffcanvasCartPageLoadedEvent $event): void
  63.     {
  64.         $isActivated $this->config['ASENextFreeDelivery.config.subshopActivated'] ?? false;
  65.         $show        $this->config['ASENextFreeDelivery.config.showInModal'] ?? false;
  66.         if (!$isActivated || !$show) {
  67.             return;
  68.         }
  69.         $event->getPage()->assign(
  70.             [
  71.                 'aseNextFreeDelivery' => [
  72.                     'difference' => $this->shippingFreeService->calculateShippingCostsDifference(
  73.                         $event->getSalesChannelContext()
  74.                     ),
  75.                 ],
  76.             ]
  77.         );
  78.     }
  79.     /**
  80.      * @param CheckoutCartPageLoadedEvent $event
  81.      *
  82.      * @throws DBALException
  83.      */
  84.     public function onCheckoutPageLoaded(CheckoutCartPageLoadedEvent $event): void
  85.     {
  86.         $isActivated $this->config['ASENextFreeDelivery.config.subshopActivated'] ?? false;
  87.         if (!$isActivated) {
  88.             return;
  89.         }
  90.         $event->getPage()->assign(
  91.             [
  92.                 'aseNextFreeDelivery' => [
  93.                     'difference' => $this->shippingFreeService->calculateShippingCostsDifference(
  94.                         $event->getSalesChannelContext()
  95.                     ),
  96.                 ],
  97.             ]
  98.         );
  99.     }
  100.     /**
  101.      * @param ProductPageLoadedEvent $event
  102.      *
  103.      * @throws DBALException
  104.      */
  105.     public function onProductLoaded(ProductPageLoadedEvent $event): void
  106.     {
  107.         $isActivated $this->config['ASENextFreeDelivery.config.subshopActivated'] ?? false;
  108.         $show        $this->config['ASENextFreeDelivery.config.showInArticle'] ?? false;
  109.         if (!$isActivated || !$show) {
  110.             return;
  111.         }
  112.         $event->getPage()->assign(
  113.             [
  114.                 'aseNextFreeDelivery' => [
  115.                     'difference' => $this->shippingFreeService->calculateShippingCostsDifference(
  116.                         $event->getSalesChannelContext()
  117.                     ),
  118.                 ],
  119.             ]
  120.         );
  121.     }
  122. }