custom/plugins/FourtwosixShippingCostsCalculator/src/Subscriber/CountriesLoadSubscriber.php line 29

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace FourtwosixShippingCostsCalculator\Subscriber;
  3. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  4. use Shopware\Core\Framework\Struct\ArrayStruct;
  5. use Shopware\Core\System\Country\SalesChannel\AbstractCountryRoute;
  6. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  7. use Shopware\Storefront\Page\Product\QuickView\MinimalQuickViewPageLoadedEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. class CountriesLoadSubscriber implements EventSubscriberInterface
  11. {
  12.     public function __construct(private AbstractCountryRoute $countryRoute
  13.     )
  14.     {
  15.     }
  16.     public static function getSubscribedEvents(): array
  17.     {
  18.         return [
  19.             GenericPageLoadedEvent::class => 'onGenericPageLoadedEvent',
  20.             MinimalQuickViewPageLoadedEvent::class => 'onMinimalQuickViewPageLoadedEvent'
  21.         ];
  22.     }
  23.     public function onGenericPageLoadedEvent(GenericPageLoadedEvent $genericPageLoadedEvent
  24.     )
  25.     {
  26.         $countries $this->countryRoute->load(new Request(), new Criteria(), $genericPageLoadedEvent->getSalesChannelContext())->getCountries();
  27.         $countries->sortByPositionAndName();
  28.         $genericPageLoadedEvent->getPage()->addExtension("FourtwosixShippingCostsCalculator", new ArrayStruct([
  29.             "countries" => $countries
  30.         ]));
  31.     }
  32.     public function onMinimalQuickViewPageLoadedEvent(MinimalQuickViewPageLoadedEvent $genericPageLoadedEvent
  33.     )
  34.     {
  35.         $countries $this->countryRoute->load(new Request(), new Criteria(), $genericPageLoadedEvent->getSalesChannelContext())->getCountries();
  36.         $countries->sortByPositionAndName();
  37.         $genericPageLoadedEvent->getPage()->addExtension("FourtwosixShippingCostsCalculator", new ArrayStruct([
  38.             "countries" => $countries
  39.         ]));
  40.     }
  41. }