custom/plugins/EsmComputer/src/Storefront/Controller/CheckoutController.php line 46

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace EsmComputer\Storefront\Controller;
  4. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  5. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  6. use Shopware\Core\System\SalesChannel\SalesChannel\ContextSwitchRoute;
  7. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  8. use Shopware\Storefront\Controller\StorefrontController;
  9. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedHook;
  10. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoader;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Shopware\Storefront\Framework\Routing\Annotation\NoStore;
  16. use function array_key_exists;
  17. /**
  18.  * @RouteScope(scopes={"storefront"})
  19.  */
  20. class CheckoutController extends StorefrontController
  21. {
  22.     private ContextSwitchRoute $contextSwitchRoute;
  23.     private CartService $cartService;
  24.     private CheckoutConfirmPageLoader $confirmPageLoader;
  25.     public function __construct(
  26.         CartService $cartService,
  27.         CheckoutConfirmPageLoader $confirmPageLoader,
  28.         ContextSwitchRoute $contextSwitchRoute
  29.     ) {
  30.         $this->cartService $cartService;
  31.         $this->confirmPageLoader $confirmPageLoader;
  32.         $this->contextSwitchRoute $contextSwitchRoute;
  33.     }
  34.     /**
  35.      * @NoStore
  36.      * @Route("/checkout/confirm", name="frontend.checkout.confirm.page", options={"seo"="false"}, methods={"GET"}, defaults={"XmlHttpRequest"=true})
  37.      */
  38.     public function confirmPage(Request $requestSalesChannelContext $context): Response
  39.     {
  40. //        @TODO Enables non logged in users to access checkout
  41. //        if (!$context->getCustomer()) {
  42. //            return $this->redirectToRoute('frontend.checkout.register.page');
  43. //        }
  44.         // Replaces Klarna with real default payment method id
  45.         if(array_key_exists('useDefaultPayment'$_SESSION) && $_SESSION['useDefaultPayment'] && $context->getCustomer()) {
  46.             $this->contextSwitchRoute->switchContext(new RequestDataBag(['paymentMethodId' => $context->getCustomer()->getDefaultPaymentMethodId()]), $context);
  47.             $context->assign([
  48.                 'paymentMethod' => $context->getCustomer()->getDefaultPaymentMethod()
  49.             ]);
  50.             unset($_SESSION['useDefaultPayment']);
  51.         }
  52.         if ($this->cartService->getCart($context->getToken(), $context)->getLineItems()->count() === 0) {
  53.             return $this->redirectToRoute('frontend.checkout.cart.page');
  54.         }
  55.         $page $this->confirmPageLoader->load($request$context);
  56.         $this->hook(new CheckoutConfirmPageLoadedHook($page$context));
  57.         $this->addCartErrors($page->getCart());
  58.         return $this->renderStorefront('@Storefront/storefront/page/checkout/confirm/index.html.twig', ['page' => $page]);
  59.     }
  60. }