custom/plugins/PremsOnePageCheckout6/src/Storefront/Page/Checkout/Subscriber/PageSubscriber.php line 89

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * PremSoft
  4.  * Copyright © 2019 Premsoft - Sven Mittreiter
  5.  *
  6.  * @copyright  Copyright (c) 2019, premsoft - Sven Mittreiter (http://www.premsoft.de)
  7.  * @author     Sven Mittreiter <info@premsoft.de>
  8.  */
  9. namespace Prems\Plugin\PremsOnePageCheckout6\Storefront\Page\Checkout\Subscriber;
  10. use Prems\Plugin\PremsOnePageCheckout6\Core\OnePageCheckout\Storefront\ConfigService;
  11. use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
  12. use Shopware\Core\Checkout\Payment\SalesChannel\AbstractPaymentMethodRoute;
  13. use Shopware\Core\Checkout\Shipping\SalesChannel\AbstractShippingMethodRoute;
  14. use Shopware\Core\Checkout\Shipping\ShippingMethodCollection;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  17. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  18. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  19. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  20. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  21. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoader;
  22. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
  23. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  24. use Symfony\Component\HttpFoundation\Request;
  25. use Symfony\Component\HttpFoundation\Session\Session;
  26. class PageSubscriber implements EventSubscriberInterface
  27. {
  28.     /**
  29.      * @var ConfigService
  30.      */
  31.     private $configService;
  32.     /**
  33.      * @var AbstractShippingMethodRoute
  34.      */
  35.     private $shippingMethodRoute;
  36.     /**
  37.      * @var AbstractPaymentMethodRoute
  38.      */
  39.     private $paymentMethodRoute;
  40.     public function __construct(
  41.         ConfigService $configService,
  42.         AbstractShippingMethodRoute $shippingMethodRoute,
  43.         AbstractPaymentMethodRoute $paymentMethodRoute
  44.     ) {
  45.         $this->configService $configService;
  46.         $this->shippingMethodRoute $shippingMethodRoute;
  47.         $this->paymentMethodRoute $paymentMethodRoute;
  48.     }
  49.     public static function getSubscribedEvents(): array
  50.     {
  51.         return [
  52.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutPageLoadedEvent',
  53.             CheckoutRegisterPageLoadedEvent::class => 'onCheckoutPageLoadedEvent',
  54.             AccountEditOrderPageLoadedEvent::class => 'onCheckoutPageLoadedEvent'
  55.         ];
  56.     }
  57.     private function getPaymentMethods(SalesChannelContext $context): PaymentMethodCollection
  58.     {
  59.         $request = new Request();
  60.         $request->query->set('onlyAvailable'true);
  61.         return $this->paymentMethodRoute
  62.             ->load($request$context, new Criteria())
  63.             ->getPaymentMethods();
  64.     }
  65.     private function getShippingMethods(SalesChannelContext $context): ShippingMethodCollection
  66.     {
  67.         $request = new Request();
  68.         $request->query->set('onlyAvailable'true);
  69.         return $this->shippingMethodRoute
  70.             ->load($request$context, new Criteria())
  71.             ->getShippingMethods();
  72.     }
  73.     /**
  74.      * @param CheckoutCartPageLoadedEvent $event
  75.      *
  76.      * @throws InconsistentCriteriaIdsException
  77.      */
  78.     public function onCheckoutPageLoadedEvent($event): void
  79.     {
  80.         $opcSettings null;
  81.         if (
  82.             $event instanceof CheckoutConfirmPageLoadedEvent ||
  83.             $event instanceof CheckoutRegisterPageLoadedEvent ||
  84.             $event instanceof AccountEditOrderPageLoadedEvent
  85.         ) {
  86.             $opcSettings $this->configService->getConfig();
  87.         } else {
  88.             return;
  89.         }
  90.         if (!$opcSettings) {
  91.             return;
  92.         }
  93.         $settingVars $opcSettings->getVars();
  94.         if (!$settingVars['useOnePageCheckout']) {
  95.             return;
  96.         }
  97.         $session = new Session();
  98.         if ($session->has('premsOpcUsedStep2')) {
  99.             $event->getPage()->assign(['premsOpcUsedStep2' => $session->get('premsOpcUsedStep2')]);
  100.         }
  101.         $context $event->getSalesChannelContext();
  102.         $request $event->getRequest();
  103.         $user $context->getCustomer();
  104.         $opcSettings->setLoggedIn(!empty($user));
  105.         $opcSettings->setLoginError((bool) $request->get('loginError'));
  106.         if (empty($user)) {
  107.             $event->getPage()->assign([
  108.                 'paymentMethods' => $this->getPaymentMethods($context),
  109.                 'shippingMethods' => $this->getShippingMethods($context)
  110.             ]);
  111.         }
  112.         if ($event instanceof AccountEditOrderPageLoadedEvent) {
  113.             $event->getPage()->assign([
  114.                 'isAccountOrderEdit' => 1,
  115.             ]);
  116.         }
  117.         // Prefill register fields with stored data
  118.         if ($event instanceof CheckoutRegisterPageLoadedEvent) {
  119.             $registerInput $session->get('PremsOnePageCheckoutRegisterInput');
  120.             if ($registerInput) {
  121.                 $dataBag $event->getRequest()->request;
  122.                 $dataBag->add($registerInput);
  123.                 $event->getPage()->assign([
  124.                     'data' => $dataBag,
  125.                 ]);
  126.             }
  127.         }
  128.         $event->getPage()->addExtension('premsOnePageCheckout'$opcSettings);
  129.     }
  130. }