<?php declare(strict_types=1);
/**
* PremSoft
* Copyright © 2019 Premsoft - Sven Mittreiter
*
* @copyright Copyright (c) 2019, premsoft - Sven Mittreiter (http://www.premsoft.de)
* @author Sven Mittreiter <info@premsoft.de>
*/
namespace Nd\Theme\Subscriber;
use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
use Shopware\Core\Checkout\Payment\SalesChannel\AbstractPaymentMethodRoute;
use Shopware\Core\Checkout\Shipping\SalesChannel\AbstractShippingMethodRoute;
use Shopware\Core\Checkout\Shipping\ShippingMethodCollection;
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoader;
use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
class ModifyCheckoutData implements EventSubscriberInterface
{
/**
* @var AbstractShippingMethodRoute
*/
private $shippingMethodRoute;
/**
* @var AbstractPaymentMethodRoute
*/
private $paymentMethodRoute;
public function __construct(
AbstractShippingMethodRoute $shippingMethodRoute,
AbstractPaymentMethodRoute $paymentMethodRoute
)
{
$this->shippingMethodRoute = $shippingMethodRoute;
$this->paymentMethodRoute = $paymentMethodRoute;
}
public static function getSubscribedEvents(): array
{
return [
CheckoutConfirmPageLoadedEvent::class => 'onCheckoutPageLoadedEvent',
CheckoutRegisterPageLoadedEvent::class => 'onCheckoutPageLoadedEvent',
AccountEditOrderPageLoadedEvent::class => 'onCheckoutPageLoadedEvent'
];
}
/**
* @param CheckoutCartPageLoadedEvent $event
*
* @throws InconsistentCriteriaIdsException
*/
public function onCheckoutPageLoadedEvent($event): void
{
$session = new Session();
$context = $event->getSalesChannelContext();
$request = $event->getRequest();
$user = $context->getCustomer();
if (empty($user)) {
$event->getPage()->assign([
'paymentMethods' => $this->getPaymentMethods($context),
'shippingMethods' => $this->getShippingMethods($context)
]);
}
if ($event instanceof AccountEditOrderPageLoadedEvent) {
$event->getPage()->assign([
'isAccountOrderEdit' => 1,
]);
}
}
private function getPaymentMethods(SalesChannelContext $context): PaymentMethodCollection
{
$request = new Request();
$request->query->set('onlyAvailable', true);
return $this->paymentMethodRoute
->load($request, $context, new Criteria())
->getPaymentMethods();
}
private function getShippingMethods(SalesChannelContext $context): ShippingMethodCollection
{
$request = new Request();
$request->query->set('onlyAvailable', true);
return $this->shippingMethodRoute
->load($request, $context, new Criteria())
->getShippingMethods();
}
}