custom/plugins/GrimmTheme/src/Storefront/Subscriber/AddCountriesToPage.php line 46

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace GrimmTheme\Storefront\Subscriber;
  3. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  4. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Shopware\Core\System\Country\CountryCollection;
  7. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\System\Country\SalesChannel\AbstractCountryRoute;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  13. class AddCountriesToPage implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var EntityRepositoryInterface
  17.      */
  18.     private $countryRepository;
  19.     /**
  20.      * @var CountryCollection
  21.      */
  22.     protected $countries;
  23.     /**
  24.      * @var AbstractCountryRoute
  25.      */
  26.     private $countryRoute;
  27.     public function __construct(AbstractCountryRoute $countryRoute)
  28.     {
  29.         $this->countryRoute $countryRoute;
  30.     }
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             ProductPageLoadedEvent::class => 'addCountries',
  35.             CheckoutCartPageLoadedEvent::class => 'addCountries'
  36.         ];
  37.     }
  38.     public function addCountries($event): void
  39.     {
  40.        
  41.         $countries $this->getCountries($event->getSalesChannelContext());
  42.         $event->getPage()->addExtension('grimm_countries'$countries);
  43.     }
  44.     private function getCountries(SalesChannelContext $salesChannelContext): CountryCollection
  45.     {
  46.         $criteria = (new Criteria())
  47.             ->addFilter(new EqualsFilter('active'true))
  48.             ->addAssociation('country.states');
  49.         $countries $this->countryRoute->load(new Request(), $criteria$salesChannelContext)->getCountries();
  50.         $countries->sortCountryAndStates();
  51.         return $countries;
  52.     }
  53. }