custom/plugins/LoyxxSeminar/src/Subscriber/CheckoutPageLoadedEventSubscriber.php line 52

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace LoyxxSeminar\Subscriber;
  3. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  6. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  7. use Shopware\Core\System\Salutation\SalesChannel\AbstractSalutationRoute;
  8. use Shopware\Core\System\Salutation\SalutationCollection;
  9. use Shopware\Core\System\Salutation\SalutationEntity;
  10. use Shopware\Core\System\SystemConfig\SystemConfigService;
  11. use Shopware\Storefront\Event\RouteRequest\SalutationRouteRequestEvent;
  12. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  13. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  14. use Shopware\Storefront\Page\PageLoadedEvent;
  15. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. class CheckoutPageLoadedEventSubscriber implements EventSubscriberInterface
  19. {
  20.     private SalesChannelRepositoryInterface $salutationRepository;
  21.     private SystemConfigService $systemConfigService;
  22.     private AbstractSalutationRoute $salutationRoute;
  23.     private EventDispatcherInterface $eventDispatcher;
  24.     public function __construct(
  25.         SalesChannelRepositoryInterface $salutationRepository,
  26.         AbstractSalutationRoute         $salutationRoute,
  27.         SystemConfigService             $systemConfigService,
  28.         EventDispatcherInterface        $eventDispatcher
  29.     )
  30.     {
  31.         $this->salutationRepository $salutationRepository;
  32.         $this->systemConfigService $systemConfigService;
  33.         $this->salutationRoute $salutationRoute;
  34.         $this->eventDispatcher $eventDispatcher;
  35.     }
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             CheckoutCartPageLoadedEvent::class => 'onCheckoutCartPageLoadedEvent',
  40.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutCartPageLoadedEvent'
  41.         ];
  42.     }
  43.     public function onCheckoutCartPageLoadedEvent(PageLoadedEvent $event)
  44.     {
  45.         if ($this->systemConfigService->getBool('LoyxxSeminar.config.isSalutationEnabled')) {
  46.             $event->getPage()->addExtensions([
  47.                 'salutations' => $this->getSalutations($event->getSalesChannelContext(), $event->getRequest())
  48.             ]);
  49.         }
  50.     }
  51.     private function getSalutations(SalesChannelContext $contextRequest $request): SalutationCollection
  52.     {
  53.         $event = new SalutationRouteRequestEvent($request, new Request(), $context, new Criteria());
  54.         $this->eventDispatcher->dispatch($event);
  55.         $salutations $this->salutationRoute
  56.             ->load($event->getStoreApiRequest(), $context$event->getCriteria())
  57.             ->getSalutations();
  58.         $salutations->sort(static function (SalutationEntity $aSalutationEntity $b) {
  59.             return $b->getSalutationKey() <=> $a->getSalutationKey();
  60.         });
  61.         return $salutations;
  62.     }
  63. }