custom/plugins/AcrisCheckoutMethodPreselectCS/src/Storefront/Subscriber/ShippingPaymentSubscriber.php line 32

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\CheckoutMethodPreselect\Storefront\Subscriber;
  3. use Acris\CheckoutMethodPreselect\Components\ValidatorCallbackService;
  4. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  5. use Shopware\Core\Checkout\Shipping\ShippingMethodEntity;
  6. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  7. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPage;
  8. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class ShippingPaymentSubscriber implements EventSubscriberInterface
  11. {
  12.     const CHECKOUT_CUSTOMER_CANCELED_EXTERNAL_PAYMENT 'CHECKOUT__CUSTOMER_CANCELED_EXTERNAL_PAYMENT';
  13.     private ValidatorCallbackService $validatorCallbackService;
  14.     public function __construct(
  15.         ValidatorCallbackService $validatorCallbackService
  16.     ) {
  17.         $this->validatorCallbackService $validatorCallbackService;
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             AccountEditOrderPageLoadedEvent::class => 'onAccountEditOrderPageLoaded'
  23.         ];
  24.     }
  25.     public function onAccountEditOrderPageLoaded(AccountEditOrderPageLoadedEvent $event): void
  26.     {
  27.         $context $event->getSalesChannelContext();
  28.         $page $event->getPage();
  29.         $this->sortPaymentMethods($page);
  30.         $paymentMethod null;
  31.         $shippingMethod null;
  32.         $contextShippingMethod = !empty($context->getShippingMethod()) ? $context->getShippingMethod() : null;
  33.         if (!empty($contextShippingMethod) && !empty($page->getSalesChannelShippingMethods()) && !$page->getSalesChannelShippingMethods()->has($contextShippingMethod->getId())) {
  34.             $shippingMethod $this->validatorCallbackService->getFirstActiveShippingMethod($context);
  35.             if($shippingMethod instanceof ShippingMethodEntity) {
  36.                 if($context->getShippingMethod() && $shippingMethod->getId() === $context->getShippingMethod()->getId()) {
  37.                     $shippingMethod null;
  38.                 }
  39.             }
  40.         }
  41.         if (!empty($event->getRequest()) && !empty($event->getRequest()->get('error-code')) && $event->getRequest()->get('error-code') === self::CHECKOUT_CUSTOMER_CANCELED_EXTERNAL_PAYMENT) {
  42.             $paymentMethod $this->validatorCallbackService->getFirstActivePaymentMethod($context);
  43.             if($paymentMethod instanceof PaymentMethodEntity) {
  44.                 if($context->getPaymentMethod() && $paymentMethod->getId() === $context->getPaymentMethod()->getId()) {
  45.                     $paymentMethod null;
  46.                 }
  47.             }
  48.         }
  49.         if($paymentMethod instanceof PaymentMethodEntity || $shippingMethod instanceof ShippingMethodEntity) {
  50.             $this->changeMethods($paymentMethod$shippingMethod$context);
  51.         }
  52.     }
  53.     private function changeMethods(?PaymentMethodEntity $paymentMethod, ?ShippingMethodEntity $shippingMethodSalesChannelContext $context): void
  54.     {
  55.         if($paymentMethod instanceof PaymentMethodEntity) {
  56.             $context->assign(['paymentMethod' => $paymentMethod]);
  57.         }
  58.         if($shippingMethod instanceof ShippingMethodEntity) {
  59.             $context->assign(['shippingMethod' => $shippingMethod]);
  60.         }
  61.     }
  62.     private function sortPaymentMethods(AccountEditOrderPage $page): void
  63.     {
  64.         $paymentMethods $page->getPaymentMethods();
  65.         if($paymentMethods && $paymentMethods->count() > 0) {
  66.             $paymentMethods->sort(function (PaymentMethodEntity $aPaymentMethodEntity $b) {
  67.                 $prioA 0;
  68.                 $prioB 0;
  69.                 if(array_key_exists('acris_checkout_payment_preselect_priority'$a->getTranslation('customFields'))) {
  70.                     $prioA = (int) $a->getTranslation('customFields')['acris_checkout_payment_preselect_priority'];
  71.                 }
  72.                 if(array_key_exists('acris_checkout_payment_preselect_priority'$b->getTranslation('customFields'))) {
  73.                     $prioB = (int) $b->getTranslation('customFields')['acris_checkout_payment_preselect_priority'];
  74.                 }
  75.                 return $prioB <=> $prioA;
  76.             });
  77.         }
  78.     }
  79. }