custom/static-plugins/NdTheme/src/Subscriber/ModifyCheckoutData.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\Subscriber;
  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 ModifyCheckoutData 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.         $session = new Session();
  59.         $context $event->getSalesChannelContext();
  60.         $request $event->getRequest();
  61.         $user $context->getCustomer();
  62.         if (empty($user)) {
  63.             $event->getPage()->assign([
  64.                 'paymentMethods' => $this->getPaymentMethods($context),
  65.                 'shippingMethods' => $this->getShippingMethods($context)
  66.             ]);
  67.         }
  68.         if ($event instanceof AccountEditOrderPageLoadedEvent) {
  69.             $event->getPage()->assign([
  70.                 'isAccountOrderEdit' => 1,
  71.             ]);
  72.         }
  73.     }
  74.     private function getPaymentMethods(SalesChannelContext $context): PaymentMethodCollection
  75.     {
  76.         $request = new Request();
  77.         $request->query->set('onlyAvailable'true);
  78.         return $this->paymentMethodRoute
  79.             ->load($request$context, new Criteria())
  80.             ->getPaymentMethods();
  81.     }
  82.     private function getShippingMethods(SalesChannelContext $context): ShippingMethodCollection
  83.     {
  84.         $request = new Request();
  85.         $request->query->set('onlyAvailable'true);
  86.         return $this->shippingMethodRoute
  87.             ->load($request$context, new Criteria())
  88.             ->getShippingMethods();
  89.     }
  90. }