custom/static-plugins/NdTheme/src/Service/CheckoutPageSubscriber.php line 64

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 Nd\Theme\Service;
  10. use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
  11. use Shopware\Core\Checkout\Payment\SalesChannel\AbstractPaymentMethodRoute;
  12. use Shopware\Core\Checkout\Shipping\SalesChannel\AbstractShippingMethodRoute;
  13. use Shopware\Core\Checkout\Shipping\ShippingMethodCollection;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  16. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  17. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  18. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  19. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  20. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoader;
  21. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
  22. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\HttpFoundation\Session\Session;
  25. class CheckoutPageSubscriber implements EventSubscriberInterface
  26. {
  27.     /**
  28.      * @var AbstractShippingMethodRoute
  29.      */
  30.     private $shippingMethodRoute;
  31.     /**
  32.      * @var AbstractPaymentMethodRoute
  33.      */
  34.     private $paymentMethodRoute;
  35.     public function __construct(
  36.         AbstractShippingMethodRoute $shippingMethodRoute,
  37.         AbstractPaymentMethodRoute  $paymentMethodRoute
  38.     )
  39.     {
  40.         $this->shippingMethodRoute $shippingMethodRoute;
  41.         $this->paymentMethodRoute $paymentMethodRoute;
  42.     }
  43.     public static function getSubscribedEvents(): array
  44.     {
  45.         return [
  46.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutPageLoadedEvent',
  47.             CheckoutRegisterPageLoadedEvent::class => 'onCheckoutPageLoadedEvent',
  48.             AccountEditOrderPageLoadedEvent::class => 'onCheckoutPageLoadedEvent'
  49.         ];
  50.     }
  51.     /**
  52.      * @param CheckoutCartPageLoadedEvent $event
  53.      *
  54.      * @throws InconsistentCriteriaIdsException
  55.      */
  56.     public function onCheckoutPageLoadedEvent($event): void
  57.     {
  58. //        if (
  59. //            $event instanceof CheckoutConfirmPageLoadedEvent ||
  60. //            $event instanceof CheckoutRegisterPageLoadedEvent ||
  61. //            $event instanceof AccountEditOrderPageLoadedEvent
  62. //        ) {
  63. //            $opcSettings = $this->configService->getConfig();
  64. //        } else {
  65. //            return;
  66. //        }
  67.         $session = new Session();
  68.         $context $event->getSalesChannelContext();
  69.         $request $event->getRequest();
  70.         $user $context->getCustomer();
  71.         if (empty($user)) {
  72.             $event->getPage()->assign([
  73.                 'paymentMethods' => $this->getPaymentMethods($context),
  74.                 'shippingMethods' => $this->getShippingMethods($context)
  75.             ]);
  76.         }
  77.         if ($event instanceof AccountEditOrderPageLoadedEvent) {
  78.             $event->getPage()->assign([
  79.                 'isAccountOrderEdit' => 1,
  80.             ]);
  81.         }
  82.     }
  83.     private function getPaymentMethods(SalesChannelContext $context): PaymentMethodCollection
  84.     {
  85.         $request = new Request();
  86.         $request->query->set('onlyAvailable'true);
  87.         return $this->paymentMethodRoute
  88.             ->load($request$context, new Criteria())
  89.             ->getPaymentMethods();
  90.     }
  91.     private function getShippingMethods(SalesChannelContext $context): ShippingMethodCollection
  92.     {
  93.         $request = new Request();
  94.         $request->query->set('onlyAvailable'true);
  95.         return $this->shippingMethodRoute
  96.             ->load($request$context, new Criteria())
  97.             ->getShippingMethods();
  98.     }
  99. }