custom/plugins/AcrisCheckoutMethodPreselectCS/src/Storefront/Subscriber/LoginPaymentMethodSubscriber.php line 34

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\CheckoutMethodPreselect\Storefront\Subscriber;
  3. use Shopware\Core\Checkout\Customer\CustomerEntity;
  4. use Shopware\Core\Framework\Validation\DataBag\DataBag;
  5. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
  6. use Shopware\Core\System\SalesChannel\Event\SalesChannelContextRestoredEvent;
  7. use Shopware\Core\System\SalesChannel\SalesChannel\SalesChannelContextSwitcher;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class LoginPaymentMethodSubscriber implements EventSubscriberInterface
  12. {
  13.     private SystemConfigService $configService;
  14.     private SalesChannelContextSwitcher $salesChannelContextSwitcher;
  15.     public function __construct(
  16.         SystemConfigService $configService,
  17.         SalesChannelContextSwitcher $salesChannelContextSwitcher
  18.     ) {
  19.         $this->configService $configService;
  20.         $this->salesChannelContextSwitcher $salesChannelContextSwitcher;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             SalesChannelContextRestoredEvent::class => 'onSalesChannelContextRestoredEvent'
  26.         ];
  27.     }
  28.     public function onSalesChannelContextRestoredEvent(SalesChannelContextRestoredEvent $event)
  29.     {
  30.         if($this->configService->get('AcrisCheckoutMethodPreselectCS.config.overruleCustomerAssignedPaymentMethod'$event->getRestoredSalesChannelContext()->getSalesChannelId()) === 'ignore') {
  31.             if($event->getCurrentSalesChannelContext() instanceof SalesChannelContext && $event->getRestoredSalesChannelContext()->getCustomer() instanceof CustomerEntity
  32.                 && $event->getRestoredSalesChannelContext()->getPaymentMethod()->getId() !== $event->getCurrentSalesChannelContext()->getPaymentMethod()->getId()
  33.             ) {
  34.                 $this->salesChannelContextSwitcher->update(new DataBag([SalesChannelContextService::PAYMENT_METHOD_ID => $event->getCurrentSalesChannelContext()->getPaymentMethod()->getId()]), $event->getRestoredSalesChannelContext());
  35.             }
  36.         }
  37.     }
  38. }