<?php declare(strict_types=1);
namespace FourtwosixShippingCostsCalculator\Subscriber;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Core\System\Country\SalesChannel\AbstractCountryRoute;
use Shopware\Storefront\Page\GenericPageLoadedEvent;
use Shopware\Storefront\Page\Product\QuickView\MinimalQuickViewPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
class CountriesLoadSubscriber implements EventSubscriberInterface
{
public function __construct(private AbstractCountryRoute $countryRoute
)
{
}
public static function getSubscribedEvents(): array
{
return [
GenericPageLoadedEvent::class => 'onGenericPageLoadedEvent',
MinimalQuickViewPageLoadedEvent::class => 'onMinimalQuickViewPageLoadedEvent'
];
}
public function onGenericPageLoadedEvent(GenericPageLoadedEvent $genericPageLoadedEvent
)
{
$countries = $this->countryRoute->load(new Request(), new Criteria(), $genericPageLoadedEvent->getSalesChannelContext())->getCountries();
$countries->sortByPositionAndName();
$genericPageLoadedEvent->getPage()->addExtension("FourtwosixShippingCostsCalculator", new ArrayStruct([
"countries" => $countries
]));
}
public function onMinimalQuickViewPageLoadedEvent(MinimalQuickViewPageLoadedEvent $genericPageLoadedEvent
)
{
$countries = $this->countryRoute->load(new Request(), new Criteria(), $genericPageLoadedEvent->getSalesChannelContext())->getCountries();
$countries->sortByPositionAndName();
$genericPageLoadedEvent->getPage()->addExtension("FourtwosixShippingCostsCalculator", new ArrayStruct([
"countries" => $countries
]));
}
}