custom/plugins/CioProductCustomerInputs/src/Subscriber/AutoChangeShippingMethodSubscriber.php line 43

Open in your IDE?
  1. <?php
  2. namespace CioProductCustomerInputs\Subscriber;
  3. use CioBudget\PaymentHandler\BudgetPayment;
  4. use CioBudget\Service\StoreLoaderService;
  5. use CioSponsoredOrderConfirmation\Core\Error\MixedSponsoredProductsError;
  6. use Shopware\Core\Checkout\Cart\Error\ErrorCollection;
  7. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  8. use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
  9. use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\DefaultPayment;
  10. use Shopware\Core\Framework\Context;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  15. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
  16. use Shopware\Core\System\SalesChannel\SalesChannel\ContextSwitchRoute;
  17. use Shopware\Storefront\Event\StorefrontRenderEvent;
  18. use Symfony\Component\DependencyInjection\ContainerInterface;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Symfony\Component\HttpFoundation\RedirectResponse;
  21. use Symfony\Component\Routing\RouterInterface;
  22. class AutoChangeShippingMethodSubscriber implements EventSubscriberInterface
  23. {
  24.     private ContainerInterface $container;
  25.     private RouterInterface $router;
  26.     public function __construct(ContainerInterface $containerRouterInterface $router)
  27.     {
  28.         $this->container $container;
  29.         $this->router $router;
  30.     }
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [
  34.             StorefrontRenderEvent::class => 'onStorefrontRender'
  35.         ];
  36.     }
  37.     public function onStorefrontRender(StorefrontRenderEvent $event)
  38.     {
  39.         $route $event->getRequest()->get('_route');
  40.         if (in_array($route, ['frontend.cart.offcanvas''frontend.checkout.confirm.page''frontend.checkout.cart.page'])) {
  41.             /** @var CartService $cartService */
  42.             $cartService $this->container->get('Shopware\Core\Checkout\Cart\SalesChannel\CartService');
  43.             $cart $cartService->getCart($event->getSalesChannelContext()->getToken(), $event->getSalesChannelContext());
  44.             if ($cart->getErrors()->count() > 1) {
  45.                 return;
  46.             }
  47.             if ($cart->getLineItems()->count() == && count($cart->getErrors()->getElements())) {
  48.                 if (!array_key_exists(MixedSponsoredProductsError::MESSAGE_KEY$cart->getErrors()->getElements())) {
  49.                     $data null;
  50.                     if (array_key_exists('shipping-method-blocked-Standard'$cart->getErrors()->getElements())) {
  51.                         $cart->setErrors(new ErrorCollection(array_filter($cart->getErrors()->getElements(), function ($key) {
  52.                             return $key !== 'shipping-method-blocked-Standard';
  53.                         }, ARRAY_FILTER_USE_KEY)));
  54.                         $data = new RequestDataBag([
  55.                             SalesChannelContextService::SHIPPING_METHOD_ID => $this->getShippingMethodId('Sponsoring Versand')
  56.                         ]);
  57.                     }
  58.                     if (array_key_exists('shipping-method-blocked-Sponsoring Versand'$cart->getErrors()->getElements())) {
  59.                         $cart->setErrors(new ErrorCollection(array_filter($cart->getErrors()->getElements(), function ($key) {
  60.                             return $key !== 'shipping-method-blocked-Sponsoring Versand';
  61.                         }, ARRAY_FILTER_USE_KEY)));
  62.                         $data = new RequestDataBag([
  63.                             SalesChannelContextService::SHIPPING_METHOD_ID => $this->getShippingMethodId('Standard')
  64.                         ]);
  65.                     }
  66.                     if (!is_null($data)) {
  67.                         /** @var ContextSwitchRoute $contextSwitchRoute */
  68.                         $contextSwitchRoute $this->container->get('Shopware\Core\System\SalesChannel\SalesChannel\ContextSwitchRoute');
  69.                         $contextSwitchRoute->switchContext($data$event->getSalesChannelContext());
  70.                         $response = new RedirectResponse($this->router->generate($route));
  71.                         $response->send();
  72.                     }
  73.                 }
  74.             } elseif (!$cart->getLineItems()->count()) {
  75.                 if (array_key_exists('shipping-method-blocked-Sponsoring Versand'$cart->getErrors()->getElements())) {
  76.                     $cart->setErrors(new ErrorCollection(array_filter($cart->getErrors()->getElements(), function ($key) {
  77.                         return $key !== 'shipping-method-blocked-Sponsoring Versand';
  78.                     }, ARRAY_FILTER_USE_KEY)));
  79.                     $data = new RequestDataBag([
  80.                         SalesChannelContextService::SHIPPING_METHOD_ID => $this->getShippingMethodId('Standard')
  81.                     ]);
  82.                     /** @var ContextSwitchRoute $contextSwitchRoute */
  83.                     $contextSwitchRoute $this->container->get('Shopware\Core\System\SalesChannel\SalesChannel\ContextSwitchRoute');
  84.                     $contextSwitchRoute->switchContext($data$event->getSalesChannelContext());
  85.                     $response = new RedirectResponse($this->router->generate($route));
  86.                     $response->send();
  87.                 }
  88.             }
  89.         }
  90.     }
  91.     protected function getShippingMethodId($name): ?string
  92.     {
  93.         /** @var EntityRepositoryInterface $repository */
  94.         $repository $this->container->get('shipping_method.repository');
  95.         $criteria = (new Criteria())
  96.             ->addFilter(new EqualsFilter('name'$name))
  97.             ->addAssociation('shipping_method_translation');
  98.         return $repository->searchIds($criteriaContext::createDefaultContext())->firstId();
  99.     }
  100. }