custom/plugins/PremsIndividualOffer6/src/Storefront/Page/Checkout/Subscriber/PageSubscriber.php line 86

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * PremSoft
  4.  * Copyright © 2020 Premsoft - Sven Mittreiter
  5.  *
  6.  * @copyright  Copyright (c) 2020, premsoft - Sven Mittreiter (http://www.premsoft.de)
  7.  * @author     Sven Mittreiter <info@premsoft.de>
  8.  */
  9. namespace Prems\Plugin\PremsIndividualOffer6\Storefront\Page\Checkout\Subscriber;
  10. use Prems\Plugin\PremsIndividualOffer6\Core\Offer\Storefront\ConfigService;
  11. use Prems\Plugin\PremsIndividualOffer6\Core\Offer\Storefront\OfferService;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  13. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  14. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  15. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  16. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  17. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use Prems\Plugin\PremsIndividualOffer6\Storefront\Page\Listing\ListingPageLoader;
  20. class PageSubscriber implements EventSubscriberInterface
  21. {
  22.     /**
  23.      * @var ConfigService
  24.      */
  25.     private $configService;
  26.     /**
  27.      * @var OfferService $offerService
  28.      */
  29.     private $offerService;
  30.     public function __construct(
  31.         ConfigService $configService,
  32.         OfferService $offerService
  33.     ) {
  34.         $this->configService $configService;
  35.         $this->offerService $offerService;
  36.     }
  37.     public static function getSubscribedEvents(): array
  38.     {
  39.         return [
  40.             CheckoutCartPageLoadedEvent::class => 'onCartPageLoadedEvent',
  41.             OffcanvasCartPageLoadedEvent::class => 'onOffcanvasCartPageLoadedEvent',
  42.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmPageLoadedEvent',
  43.             //CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishPageLoadedEvent',
  44.         ];
  45.     }
  46.     /**
  47.      * @param CheckoutFinishPageLoadedEvent $event
  48.      */
  49.     /**public function onCheckoutFinishPageLoadedEvent($event): void
  50.     {
  51.         $request = $event->getRequest();
  52.         $context = $event->getSalesChannelContext();
  53.         $offerSettings = null;
  54.         if ($event instanceof CheckoutFinishPageLoadedEvent) {
  55.             $offerSettings = $this->configService->getConfig($context);
  56.         } else {
  57.             return;
  58.         }
  59.         if (!$offerSettings) {
  60.             return;
  61.         }
  62.         if ($this->offerService->getOfferMode($context)) {
  63.             $offer = $this->offerService->getOfferById($this->offerService->getOfferMode($context), $event->getContext());
  64.             $this->offerService->disableOfferMode($context);
  65.             $this->offerService->acceptOffer($offer, $event->getContext());
  66.         }
  67.     }*/
  68.     /**
  69.      * @param CheckoutConfirmPageLoadedEvent $event
  70.      *
  71.      * @throws InconsistentCriteriaIdsException
  72.      */
  73.     public function onCheckoutConfirmPageLoadedEvent(CheckoutConfirmPageLoadedEvent $event): void
  74.     {
  75.         $request $event->getRequest();
  76.         $context $event->getSalesChannelContext();
  77.         $offerSettings $this->configService->getConfig($context);
  78.         if (!$offerSettings) {
  79.             return;
  80.         }
  81.         $offerMode $this->offerService->getOfferMode($context$request->getSession());
  82.         if ($offerMode) {
  83.             $customer $context->getCustomer();
  84.             if (!$customer) {
  85.                 return;
  86.             }
  87.             $offer $this->offerService->getOfferById($offerMode$context->getContext(), $customer);
  88.             if (!$offer) {
  89.                 return;
  90.             }
  91.             $event->getPage()->assign(['offer' => $offer]);
  92.             $event->getPage()->assign(['offerMode' => $offerMode]);
  93.         }
  94.         $event->getPage()->addExtension('premsOffer'$offerSettings);
  95.     }
  96.     /**
  97.      * @param CheckoutCartPageLoadedEvent $event
  98.      *
  99.      * @throws InconsistentCriteriaIdsException
  100.      */
  101.     public function onCartPageLoadedEvent(CheckoutCartPageLoadedEvent $event): void
  102.     {
  103.         $request $event->getRequest();
  104.         $context $event->getSalesChannelContext();
  105.         $offerSettings $this->configService->getConfig($context);
  106.         if (!$offerSettings) {
  107.             return;
  108.         }
  109.         $offerMode $this->offerService->getOfferMode($context$request->getSession());
  110.         if ($offerMode) {
  111.             $customer $context->getCustomer();
  112.             if (!$customer) {
  113.                 return;
  114.             }
  115.             $offer $this->offerService->getOfferById($offerMode$context->getContext(), $customer);
  116.             if (!$offer) {
  117.                 return;
  118.             }
  119.             $event->getPage()->assign(['offer' => $offer]);
  120.             $event->getPage()->assign(['offerMode' => $offerMode]);
  121.         }
  122.         $settingVars $offerSettings->getVars();
  123.         if (!$settingVars['showOfferButtonInCart'] || $offerMode) {
  124.             return;
  125.         }
  126.         $event->getPage()->addExtension('premsOffer'$offerSettings);
  127.     }
  128.     /**
  129.      * @param OffcanvasCartPageLoadedEvent $event
  130.      *
  131.      * @throws InconsistentCriteriaIdsException
  132.      */
  133.     public function onOffcanvasCartPageLoadedEvent(OffcanvasCartPageLoadedEvent $event): void
  134.     {
  135.         $request $event->getRequest();
  136.         $context $event->getSalesChannelContext();
  137.         $offerSettings $this->configService->getConfig($context);
  138.         $offerMode $this->offerService->getOfferMode($context$request->getSession());
  139.         if ($offerMode) {
  140.             $event->getPage()->assign(['offerMode' => $offerMode]);
  141.         }
  142.         if (!$offerSettings) {
  143.             return;
  144.         }
  145.         $settingVars $offerSettings->getVars();
  146.         if (!$settingVars['showOfferButtonInOffcanvasCart'] || $offerMode) {
  147.             return;
  148.         }
  149.         $event->getPage()->addExtension('premsOffer'$offerSettings);
  150.     }
  151. }