<?php declare(strict_types=1);
namespace Nxs\NxsPayoneModification\Communication\Checkout;
use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Shopware\Storefront\Page\PageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CheckoutConfirmEventSubscriber implements EventSubscriberInterface
{
private const PAY_ONE_EXTENSION = 'payone';
private const TEMPLATE_PROPERTY = 'template';
public static function getSubscribedEvents()
{
return [
CheckoutConfirmPageLoadedEvent::class => 'checkoutConfirmEventLoader',
AccountEditOrderPageLoadedEvent::class => 'checkoutConfirmEventLoader',
];
}
public function checkoutConfirmEventLoader(PageLoadedEvent $event)
{
$page = $event->getPage();
if ($page->hasExtension(self::PAY_ONE_EXTENSION)) {
$payoneExtension = $page->getExtension(self::PAY_ONE_EXTENSION);
$this->assignPayoneTemplateToCustomTemplate($payoneExtension);
}
}
private function assignPayoneTemplateToCustomTemplate($payoneExtension)
{
if (!empty($payoneExtension->getVars()[self::TEMPLATE_PROPERTY])) {
$templateName = $payoneExtension->getVars()[self::TEMPLATE_PROPERTY];
if (strpos($templateName, 'payolution-invoicing-form') || strpos($templateName, 'payolution-installment-form')) {
$payoneExtension->assign([self::TEMPLATE_PROPERTY => substr_replace($templateName, "-fixed", -10) . '.html.twig']);
}
}
}
}