custom/plugins/NetiNextFreeDelivery/src/Subscriber/Frontend.php line 81

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace NetInventors\NetiNextFreeDelivery\Subscriber;
  3. use NetInventors\NetiNextFreeDelivery\Components\ShippingFreeService;
  4. use NetInventors\NetiNextFreeDelivery\Service\PluginConfig;
  5. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  6. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  7. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  8. use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class Frontend implements EventSubscriberInterface
  11. {
  12.     private PluginConfig        $pluginConfig;
  13.     private ShippingFreeService $shippingFreeService;
  14.     public function __construct(
  15.         PluginConfig        $pluginConfig,
  16.         ShippingFreeService $shippingFreeService
  17.     ) {
  18.         $this->pluginConfig        $pluginConfig;
  19.         $this->shippingFreeService $shippingFreeService;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             ProductPageLoadedEvent::class       => 'onProductLoaded',
  25.             CheckoutCartPageLoadedEvent::class  => 'onCheckoutPageLoaded',
  26.             OffcanvasCartPageLoadedEvent::class => 'onAjaxCartPageLoaded',
  27.             HeaderPageletLoadedEvent::class     => 'headerLoadedEvent',
  28.         ];
  29.     }
  30.     /**
  31.      * @param HeaderPageletLoadedEvent $event
  32.      */
  33.     public function headerLoadedEvent(HeaderPageletLoadedEvent $event): void
  34.     {
  35.         $isActivated $this->pluginConfig->isSubshopActivated();
  36.         $show        $this->pluginConfig->isShowInHeader();
  37.         if (!$isActivated || !$show) {
  38.             return;
  39.         }
  40.         $event->getPagelet()->addExtension(
  41.             'netiNextFreeDelivery',
  42.             $this->shippingFreeService->calculateShippingCostsDifference(
  43.                 $event->getSalesChannelContext()
  44.             ),
  45.         );
  46.     }
  47.     /**
  48.      * @param OffcanvasCartPageLoadedEvent $event
  49.      */
  50.     public function onAjaxCartPageLoaded(OffcanvasCartPageLoadedEvent $event): void
  51.     {
  52.         $isActivated $this->pluginConfig->isSubshopActivated();
  53.         $show        $this->pluginConfig->isShowInModal();
  54.         if (!$isActivated || !$show) {
  55.             return;
  56.         }
  57.         $event->getPage()->assign(
  58.             [
  59.                 'netiNextFreeDelivery' => $this->shippingFreeService->calculateShippingCostsDifference(
  60.                     $event->getSalesChannelContext()
  61.                 ),
  62.             ]
  63.         );
  64.     }
  65.     /**
  66.      * @param CheckoutCartPageLoadedEvent $event
  67.      */
  68.     public function onCheckoutPageLoaded(CheckoutCartPageLoadedEvent $event): void
  69.     {
  70.         $isActivated $this->pluginConfig->isSubshopActivated();
  71.         if (!$isActivated) {
  72.             return;
  73.         }
  74.         $event->getPage()->assign(
  75.             [
  76.                 'netiNextFreeDelivery' => $this->shippingFreeService->calculateShippingCostsDifference(
  77.                     $event->getSalesChannelContext()
  78.                 ),
  79.             ]
  80.         );
  81.     }
  82.     /**
  83.      * @param ProductPageLoadedEvent $event
  84.      */
  85.     public function onProductLoaded(ProductPageLoadedEvent $event): void
  86.     {
  87.         $isActivated $this->pluginConfig->isSubshopActivated();
  88.         $show        $this->pluginConfig->isShowInArticle();
  89.         if (!$isActivated || !$show) {
  90.             return;
  91.         }
  92.         $event->getPage()->assign(
  93.             [
  94.                 'netiNextFreeDelivery' => $this->shippingFreeService->calculateShippingCostsDifference(
  95.                     $event->getSalesChannelContext()
  96.                 ),
  97.             ]
  98.         );
  99.     }
  100. }