custom/plugins/b2bsellerscore/src/Components/ExpressCheckout/Subscriber/CheckoutSubscriber.php line 51

Open in your IDE?
  1. <?php
  2. namespace B2bSellersCore\Components\ExpressCheckout\Subscriber;
  3. use B2bSellersCore\Components\B2bPlatform\Traits\B2bContextTrait;
  4. use B2bSellersCore\Components\ExpressCheckout\SalesChannel\AbstractExpressCheckoutSettingRoute;
  5. use LogicException;
  6. use Shopware\Core\Framework\Adapter\Translation\AbstractTranslator;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\System\SystemConfig\SystemConfigService;
  9. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  10. use Symfony\Component\DependencyInjection\ContainerInterface;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
  13. use Symfony\Component\HttpFoundation\Request;
  14. class CheckoutSubscriber implements EventSubscriberInterface
  15. {
  16.     use B2bContextTrait;
  17.     private EntityRepositoryInterface $expressCheckoutSettingRepository;
  18.     private ContainerInterface $container;
  19.     private AbstractTranslator $translator;
  20.     private SystemConfigService $configService;
  21.     private AbstractExpressCheckoutSettingRoute $expressCheckoutSettingRoute;
  22.     public function __construct(
  23.         EntityRepositoryInterface           $expressCheckoutSettingRepository,
  24.         ContainerInterface                  $container,
  25.         AbstractTranslator                  $translator,
  26.         SystemConfigService                 $configService,
  27.         AbstractExpressCheckoutSettingRoute $expressCheckoutSettingRoute)
  28.     {
  29.         $this->expressCheckoutSettingRepository $expressCheckoutSettingRepository;
  30.         $this->container $container;
  31.         $this->translator $translator;
  32.         $this->configService $configService;
  33.         $this->expressCheckoutSettingRoute $expressCheckoutSettingRoute;
  34.     }
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [
  38.             CheckoutCartPageLoadedEvent::class => 'checkExpressCheckoutSetting'
  39.         ];
  40.     }
  41.     public function checkExpressCheckoutSetting($event): void
  42.     {
  43.         $customer $event->getSalesChannelContext()->getCustomer();
  44.         if ($this->hasB2bPlatformContext($event->getSalesChannelContext()) && $customer) {
  45.             if (!$this->configService->get('B2bSellersCore.config.enableExpressCheckout'$event->getSalesChannelContext()->getSalesChannelId())) {
  46.                 return;
  47.             }
  48.             $expressCheckoutSetting $this->expressCheckoutSettingRoute->get(new Request(), $event->getSalesChannelContext())->getExpressCheckoutSetting();
  49.             if ($expressCheckoutSetting === null) {
  50.                 $this->addFlash('info'$this->translator->trans('b2bPlatform.expressCheckout.infoMessage'));
  51.             }
  52.             if ($expressCheckoutSetting !== null && ($expressCheckoutSetting->getBillingAddress() === null || $expressCheckoutSetting->getShippingAddress() === null || $expressCheckoutSetting->getPaymentMethod() === null)) {
  53.                 $this->addFlash('info'$this->translator->trans('b2bPlatform.expressCheckout.infoMessage'));
  54.             }
  55.         }
  56.     }
  57.     /**
  58.      * Adds a flash message to the current session for type.
  59.      *
  60.      * @throws LogicException
  61.      */
  62.     protected function addFlash(string $type$message): void
  63.     {
  64.         try {
  65.             $this->container->get('session')->getFlashBag()->add($type$message);
  66.         } catch (SessionNotFoundException $e) {
  67.             throw new LogicException('You can not use the addFlash method if sessions are disabled. Enable them in "config/packages/framework.yaml".'0$e);
  68.         }
  69.     }
  70. }