custom/plugins/FourtwosixChooseDeliveryCountryInSideCart/src/Subscriber/AddDataToOffcanvas.php line 38

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace FourtwosixChooseDeliveryCountryInSideCart\Subscriber;
  3. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  6. use Shopware\Core\System\Country\SalesChannel\AbstractCountryRoute;
  7. use Shopware\Core\System\Country\CountryCollection;
  8. use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
  9. use Shopware\Core\Checkout\Payment\SalesChannel\AbstractPaymentMethodRoute;
  10. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. class AddDataToOffcanvas implements EventSubscriberInterface
  14. {
  15.     private AbstractCountryRoute $countryRoute;
  16.     private AbstractPaymentMethodRoute $paymentMethodRoute;
  17.     public function __construct(
  18.         AbstractCountryRoute $countryRoute,
  19.         AbstractPaymentMethodRoute $paymentMethodRoute
  20.     )
  21.     {
  22.         $this->countryRoute $countryRoute;
  23.         $this->paymentMethodRoute $paymentMethodRoute;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             OffcanvasCartPageLoadedEvent::class => 'onOffcanvasCartLoaded'
  29.         ];
  30.     }
  31.     public function onOffcanvasCartLoaded(OffcanvasCartPageLoadedEvent $event)
  32.     {
  33.         $countries $this->getCountries($event->getSalesChannelContext());
  34.         $paymentMethods $this->getPaymentMethods($event->getSalesChannelContext());
  35.         
  36.         $event->getPage()->addExtension(
  37.             'availableCountries',
  38.             $countries
  39.         );
  40.         $event->getPage()->addExtension(
  41.             'availablePaymentMethods',
  42.             $paymentMethods
  43.         );
  44.     }
  45.     private function getCountries(SalesChannelContext $context): CountryCollection
  46.     {
  47.         $countries $this->countryRoute->load(new Request(), new Criteria(), $context)->getCountries();
  48.         $countries->sortByPositionAndName();
  49.         return $countries;
  50.     }
  51.     private function getPaymentMethods(SalesChannelContext $context): PaymentMethodCollection
  52.     {
  53.         $request = new Request();
  54.         $request->query->set('onlyAvailable''1');
  55.         return $this->paymentMethodRoute->load($request$context, new Criteria())->getPaymentMethods();
  56.     }
  57. }