<?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 Prems\Plugin\PremsOnePageCheckout6\Storefront\Page\Checkout\Subscriber;
use Prems\Plugin\PremsOnePageCheckout6\Core\OnePageCheckout\Storefront\ConfigService;
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 PageSubscriber implements EventSubscriberInterface
{
/**
* @var ConfigService
*/
private $configService;
/**
* @var AbstractShippingMethodRoute
*/
private $shippingMethodRoute;
/**
* @var AbstractPaymentMethodRoute
*/
private $paymentMethodRoute;
public function __construct(
ConfigService $configService,
AbstractShippingMethodRoute $shippingMethodRoute,
AbstractPaymentMethodRoute $paymentMethodRoute
) {
$this->configService = $configService;
$this->shippingMethodRoute = $shippingMethodRoute;
$this->paymentMethodRoute = $paymentMethodRoute;
}
public static function getSubscribedEvents(): array
{
return [
CheckoutConfirmPageLoadedEvent::class => 'onCheckoutPageLoadedEvent',
CheckoutRegisterPageLoadedEvent::class => 'onCheckoutPageLoadedEvent',
AccountEditOrderPageLoadedEvent::class => 'onCheckoutPageLoadedEvent'
];
}
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();
}
/**
* @param CheckoutCartPageLoadedEvent $event
*
* @throws InconsistentCriteriaIdsException
*/
public function onCheckoutPageLoadedEvent($event): void
{
$opcSettings = null;
if (
$event instanceof CheckoutConfirmPageLoadedEvent ||
$event instanceof CheckoutRegisterPageLoadedEvent ||
$event instanceof AccountEditOrderPageLoadedEvent
) {
$opcSettings = $this->configService->getConfig();
} else {
return;
}
if (!$opcSettings) {
return;
}
$settingVars = $opcSettings->getVars();
if (!$settingVars['useOnePageCheckout']) {
return;
}
$session = new Session();
if ($session->has('premsOpcUsedStep2')) {
$event->getPage()->assign(['premsOpcUsedStep2' => $session->get('premsOpcUsedStep2')]);
}
$context = $event->getSalesChannelContext();
$request = $event->getRequest();
$user = $context->getCustomer();
$opcSettings->setLoggedIn(!empty($user));
$opcSettings->setLoginError((bool) $request->get('loginError'));
if (empty($user)) {
$event->getPage()->assign([
'paymentMethods' => $this->getPaymentMethods($context),
'shippingMethods' => $this->getShippingMethods($context)
]);
}
if ($event instanceof AccountEditOrderPageLoadedEvent) {
$event->getPage()->assign([
'isAccountOrderEdit' => 1,
]);
}
// Prefill register fields with stored data
if ($event instanceof CheckoutRegisterPageLoadedEvent) {
$registerInput = $session->get('PremsOnePageCheckoutRegisterInput');
if ($registerInput) {
$dataBag = $event->getRequest()->request;
$dataBag->add($registerInput);
$event->getPage()->assign([
'data' => $dataBag,
]);
}
}
$event->getPage()->addExtension('premsOnePageCheckout', $opcSettings);
}
}