<?php
declare(strict_types=1);
namespace EsmComputer\Storefront\Controller;
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
use Shopware\Core\System\SalesChannel\SalesChannel\ContextSwitchRoute;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\StorefrontController;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedHook;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoader;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Symfony\Component\Routing\Annotation\Route;
use Shopware\Storefront\Framework\Routing\Annotation\NoStore;
use function array_key_exists;
/**
* @RouteScope(scopes={"storefront"})
*/
class CheckoutController extends StorefrontController
{
private ContextSwitchRoute $contextSwitchRoute;
private CartService $cartService;
private CheckoutConfirmPageLoader $confirmPageLoader;
public function __construct(
CartService $cartService,
CheckoutConfirmPageLoader $confirmPageLoader,
ContextSwitchRoute $contextSwitchRoute
) {
$this->cartService = $cartService;
$this->confirmPageLoader = $confirmPageLoader;
$this->contextSwitchRoute = $contextSwitchRoute;
}
/**
* @NoStore
* @Route("/checkout/confirm", name="frontend.checkout.confirm.page", options={"seo"="false"}, methods={"GET"}, defaults={"XmlHttpRequest"=true})
*/
public function confirmPage(Request $request, SalesChannelContext $context): Response
{
// @TODO Enables non logged in users to access checkout
// if (!$context->getCustomer()) {
// return $this->redirectToRoute('frontend.checkout.register.page');
// }
// Replaces Klarna with real default payment method id
if(array_key_exists('useDefaultPayment', $_SESSION) && $_SESSION['useDefaultPayment'] && $context->getCustomer()) {
$this->contextSwitchRoute->switchContext(new RequestDataBag(['paymentMethodId' => $context->getCustomer()->getDefaultPaymentMethodId()]), $context);
$context->assign([
'paymentMethod' => $context->getCustomer()->getDefaultPaymentMethod()
]);
unset($_SESSION['useDefaultPayment']);
}
if ($this->cartService->getCart($context->getToken(), $context)->getLineItems()->count() === 0) {
return $this->redirectToRoute('frontend.checkout.cart.page');
}
$page = $this->confirmPageLoader->load($request, $context);
$this->hook(new CheckoutConfirmPageLoadedHook($page, $context));
$this->addCartErrors($page->getCart());
return $this->renderStorefront('@Storefront/storefront/page/checkout/confirm/index.html.twig', ['page' => $page]);
}
}