<?php declare(strict_types=1);
namespace Okeonline\KejeDeliveryAddress\Storefront\Subscriber;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\System\SalesChannel\SalesChannelEntity;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CheckoutConfirmPageRequestSubscriber implements EventSubscriberInterface
{
/** @var EntityRepository $salesChannelRepository */
private $salesChannelRepository;
public function __construct(
EntityRepository $salesChannelRepository
)
{
$this->salesChannelRepository = $salesChannelRepository;
}
public static function getSubscribedEvents(): array
{
return [
CheckoutConfirmPageLoadedEvent::class => 'addCountriesToPage'
];
}
// adds countries to the confirm page, for maunal adress input
public function addCountriesToPage(CheckoutConfirmPageLoadedEvent $event)
{
$page = $event->getPage();
$currentSalesChannelId = $event->getSalesChannelContext()->getSalesChannel()->getId();
$criteria = new Criteria([$currentSalesChannelId]);
$criteria->addAssociation('countries');
$result = $this->salesChannelRepository->search($criteria, $event->getContext());
if($result->count() !== 1)
return;
/** @var SalesChannelEntity */
$salesChannel = $result->first();
$countries = $salesChannel->getCountries();
$page->addExtension('countries', $countries);
}
}