custom/plugins/ProxaCheckoutTemplate/src/Subscriber/Storefront/CheckoutSubscriber.php line 182

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace ProxaCheckoutTemplate\Subscriber\Storefront;
  3. use Shopware\Core\Checkout\Customer\CustomerEntity;
  4. use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
  5. use Shopware\Core\Checkout\Customer\Event\GuestCustomerRegisterEvent;
  6. use Shopware\Core\Content\Newsletter\SalesChannel\AbstractNewsletterSubscribeRoute;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. use Shopware\Core\Framework\Struct\ArrayEntity;
  12. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  13. use Shopware\Core\System\Country\CountryCollection;
  14. use Shopware\Core\System\Country\SalesChannel\AbstractCountryRoute;
  15. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  16. use Shopware\Core\System\Salutation\SalesChannel\AbstractSalutationRoute;
  17. use Shopware\Core\System\Salutation\SalutationCollection;
  18. use Shopware\Core\System\Salutation\SalutationEntity;
  19. use Shopware\Core\System\SystemConfig\SystemConfigService;
  20. use Shopware\Storefront\Framework\Routing\RequestTransformer;
  21. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  22. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
  23. use Shopware\Storefront\Page\PageLoadedEvent;
  24. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  25. use Symfony\Component\HttpFoundation\Request;
  26. use Symfony\Component\DependencyInjection\ContainerInterface;
  27. class CheckoutSubscriber implements EventSubscriberInterface
  28. {
  29.     private const PLUGIN_CONFIG_LINES_COUNT 3;
  30.     /**
  31.      * @var AbstractSalutationRoute
  32.      */
  33.     private $salutationRoute;
  34.     /**
  35.      * @var AbstractCountryRoute
  36.      */
  37.     private $countryRoute;
  38.     /**
  39.      * @var SystemConfigService
  40.      */
  41.     private $systemConfigService;
  42.     /**
  43.      * @var EntityRepositoryInterface
  44.      */
  45.     private $mediaRepository;
  46.     /**
  47.      * @var ContainerInterface
  48.      */
  49.     private $container;
  50.     /**
  51.      * @var AbstractNewsletterSubscribeRoute
  52.      */
  53.     private $newsletterSubscribeRoute;
  54.     /**
  55.      * @var EntityRepositoryInterface
  56.      */
  57.     private $customerRepository;
  58.     /**
  59.      * @var EntityRepositoryInterface
  60.      */
  61.     private $pluginRepository;
  62.     /**
  63.      * @var array
  64.      */
  65.     private $pluginConfigs;
  66.     /**
  67.      * @var string
  68.      */
  69.     private $pluginName;
  70.     /**
  71.      * CheckoutSubscriber constructor.
  72.      * @param AbstractSalutationRoute $salutationRoute
  73.      * @param AbstractCountryRoute $countryRoute
  74.      * @param SystemConfigService $systemConfigService
  75.      * @param EntityRepositoryInterface $mediaRepository
  76.      * @param ContainerInterface $container
  77.      * @param AbstractNewsletterSubscribeRoute $newsletterSubscribeRoute
  78.      * @param EntityRepositoryInterface $customerRepository
  79.      * @param EntityRepositoryInterface $pluginRepository
  80.      * @param string $pluginName
  81.      */
  82.     public function __construct(
  83.         AbstractSalutationRoute $salutationRoute,
  84.         AbstractCountryRoute $countryRoute,
  85.         SystemConfigService $systemConfigService,
  86.         EntityRepositoryInterface $mediaRepository,
  87.         ContainerInterface $container,
  88.         AbstractNewsletterSubscribeRoute $newsletterSubscribeRoute,
  89.         EntityRepositoryInterface $customerRepository,
  90.         EntityRepositoryInterface $pluginRepository,
  91.         string $pluginName
  92.     )
  93.     {
  94.         $this->salutationRoute $salutationRoute;
  95.         $this->countryRoute $countryRoute;
  96.         $this->systemConfigService $systemConfigService;
  97.         $this->mediaRepository $mediaRepository;
  98.         $this->container $container;
  99.         $this->newsletterSubscribeRoute $newsletterSubscribeRoute;
  100.         $this->customerRepository $customerRepository;
  101.         $this->pluginRepository $pluginRepository;
  102.         $this->pluginName $pluginName;
  103.     }
  104.     /**
  105.      * @return array
  106.      */
  107.     public static function getSubscribedEvents(): array
  108.     {
  109.         return [
  110.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmPageLoaded',
  111.             CheckoutRegisterPageLoadedEvent::class => 'onCheckoutRegisterPageLoaded',
  112.             CustomerRegisterEvent::class => 'onCustomerRegister',
  113.             GuestCustomerRegisterEvent::class => 'onCustomerRegister'
  114.         ];
  115.     }
  116.     public function onCustomerRegister(CustomerRegisterEvent $event)
  117.     {
  118.         $customer $event->getCustomer();
  119.         $request $this->container->get('request_stack')->getCurrentRequest();
  120.         if (!empty($request->get("option"))) {
  121.             $dataBag $this->getDataForSubscribe($request->get("option"), $customer$request->attributes->get(RequestTransformer::STOREFRONT_URL));
  122.             $this->newsletterSubscribeRoute->subscribe(
  123.                 $dataBag,
  124.                 $event->getSalesChannelContext(),
  125.                 false
  126.             );
  127.             $customer->setNewsletter(true);
  128.             $this->customerRepository->update(
  129.                 [
  130.                     ['id' => $customer->getId(), 'newsletter' => true],
  131.                 ],
  132.                 $event->getSalesChannelContext()->getContext()
  133.             );
  134.         }
  135.     }
  136.     /**
  137.      * @param string $subscribeOption
  138.      * @param CustomerEntity $customer
  139.      * @param string $storefrontUrl
  140.      * @return RequestDataBag
  141.      */
  142.     private function getDataForSubscribe(string $subscribeOptionCustomerEntity $customerstring $storefrontUrl)
  143.     {
  144.         return (new RequestDataBag([
  145.             "storefrontUrl" => $storefrontUrl,
  146.             "option" => $subscribeOption,
  147.             "email" => $customer->getEmail(),
  148.             "salutationId" => $customer->getSalutationId(),
  149.             "title" => $customer->getTitle(),
  150.             "firstName" => $customer->getFirstName(),
  151.             "lastName" => $customer->getLastName(),
  152.             "zipCode" => $customer->getDefaultShippingAddress()->getZipCode(),
  153.             "city" => $customer->getDefaultShippingAddress()->getCity(),
  154.             "street" => ($customer->getDefaultShippingAddress() ? $customer->getDefaultShippingAddress()->getStreet() : null)
  155.         ]));
  156.     }
  157.     /**
  158.      * @param PageLoadedEvent $event
  159.      */
  160.     public function onCheckoutConfirmPageLoaded(PageLoadedEvent $event): void
  161.     {
  162.         $request $event->getRequest();
  163.         $proxaPayment $request->query->get("proxaPayment");
  164.         $proxaShipping $request->query->get("proxaShipping");
  165.         $proxaFromPayment $request->query->get("proxaFromPayment");
  166.         if ($proxaPayment) {
  167.             $event->getPage()->addExtension("proxaPayment", new ArrayEntity([1]));
  168.         }
  169.         if ($proxaShipping) {
  170.             $event->getPage()->addExtension("proxaShipping", new ArrayEntity([1]));
  171.         }
  172.         if ($proxaFromPayment) {
  173.             $event->getPage()->addExtension("proxaFromPayment", new ArrayEntity([1]));
  174.         }
  175.         if ($this->checkPluginIsActive($event->getContext(), "KlarnaPayment")) {
  176.             $event->getPage()->addExtension("proxaKlarnaPluginIsActive", new ArrayEntity([1]));
  177.         }
  178.         if ($this->checkPluginIsActive($event->getContext(), "StripeShopwarePayment")) {
  179.             $event->getPage()->addExtension("stripeShopwarePaymentIsActive", new ArrayEntity([1]));
  180.         }
  181.         $event->getPage()->assign([
  182.             'salutations' => $this->getSalutations($event->getSalesChannelContext()),
  183.             'countries' => $this->getCountries($event->getSalesChannelContext())
  184.         ]);
  185.         $this->addExtensionsToPage($event);
  186.     }
  187.     /**
  188.      * @param PageLoadedEvent $event
  189.      */
  190.     public function onCheckoutRegisterPageLoaded(PageLoadedEvent $event)
  191.     {
  192.         $this->addExtensionsToPage($event);
  193.     }
  194.     /**
  195.      * @param PageLoadedEvent $event
  196.      */
  197.     public function addExtensionsToPage(PageLoadedEvent $event)
  198.     {
  199.         $this->pluginConfigs $this->getPluginConfig(''$event->getSalesChannelContext()->getSalesChannelId());
  200.         $event->getPage()->addExtension("proxaCheckoutInfoLines", new ArrayEntity($this->getCheckoutInfoLines($event->getContext())));
  201.         if ($this->pluginConfigs) {
  202.             if (!empty($this->pluginConfigs["removeCheckoutHeaderFooter"])) {
  203.                 $event->getPage()->addExtension("proxaRemoveCheckoutHeaderFooter", new ArrayEntity([$this->pluginConfigs["removeCheckoutHeaderFooter"]]));
  204.             }
  205.             if (!empty($this->pluginConfigs["showNewsletterRegistration"])) {
  206.                 $event->getPage()->addExtension("proxaShowNewsletterRegistration", new ArrayEntity([$this->pluginConfigs["showNewsletterRegistration"]]));
  207.             }
  208.         }
  209.     }
  210.     /**
  211.      * @param SalesChannelContext $context
  212.      * @return SalutationCollection
  213.      */
  214.     private function getSalutations(SalesChannelContext $context): SalutationCollection
  215.     {
  216.         $salutations $this->salutationRoute->load(new Request(), $context, new Criteria())->getSalutations();
  217.         $salutations->sort(function (SalutationEntity $aSalutationEntity $b) {
  218.             return $b->getSalutationKey() <=> $a->getSalutationKey();
  219.         });
  220.         return $salutations;
  221.     }
  222.     /**
  223.      * @param SalesChannelContext $salesChannelContext
  224.      * @return CountryCollection
  225.      */
  226.     private function getCountries(SalesChannelContext $salesChannelContext): CountryCollection
  227.     {
  228.         $criteria = (new Criteria())
  229.             ->addFilter(new EqualsFilter('country.active'true))
  230.             ->addAssociation('states');
  231.         $countries $this->countryRoute
  232.             ->load(new Request(), $criteria$salesChannelContext)
  233.             ->getCountries();
  234.         $countries->sortCountryAndStates();
  235.         return $countries;
  236.     }
  237.     /**
  238.      * @param Context $context
  239.      * @return array
  240.      */
  241.     private function getCheckoutInfoLines(Context $context)
  242.     {
  243.         $data = [];
  244.         if (!empty($this->pluginConfigs)) {
  245.             for ($currentLine 1$currentLine <= self::PLUGIN_CONFIG_LINES_COUNT$currentLine++) {
  246.                 $configMediaLabel "checkoutMedia" $currentLine;
  247.                 $row = [];
  248.                 if (!empty($this->pluginConfigs[$configMediaLabel])) {
  249.                     $media $this->mediaRepository->search((new Criteria([$this->pluginConfigs[$configMediaLabel]])), $context)->first();
  250.                     if ($media) {
  251.                         $row["media"] = $media->getUrl();
  252.                     }
  253.                 }
  254.                 $data[] = $row;
  255.             }
  256.         }
  257.         return $data;
  258.     }
  259.     /**
  260.      * @param Context $context
  261.      * @param string $pluginName
  262.      * @return bool
  263.      */
  264.     private function checkPluginIsActive(Context $contextstring $pluginName): bool
  265.     {
  266.         $criteria = (new Criteria())
  267.             ->addFilter(new EqualsFilter('name'$pluginName))
  268.             ->addFilter(new EqualsFilter('active'true));
  269.         return ($this->pluginRepository->search($criteria$context)->count() !== 0);
  270.     }
  271.     /**
  272.      * @param string $name
  273.      * @param mixed $channelId
  274.      * @param mixed $default
  275.      * @return mixed
  276.      */
  277.     public function getPluginConfig(string $name ''$channelId null$default null)
  278.     {
  279.         $domain $this->systemConfigService->getDomain($this->pluginName$channelIdtrue);
  280.         $keys array_map(function ($key) {
  281.             return str_replace($this->pluginName '.config.'''$key);
  282.         }, array_keys($domain));
  283.         $config array_combine($keysarray_values($domain));
  284.         if (!empty($name)) {
  285.             return isset($config[$name]) ? $config[$name] : $default;
  286.         }
  287.         return $config;
  288.     }
  289. }