custom/plugins/WbfkExtensions/src/Subscriber/AddAffiliateInformationToSalesChannelContext.php line 37

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace WbfkExtensions\Subscriber;
  4. use Shopware\Core\Checkout\Order\SalesChannel\OrderService;
  5. use Shopware\Core\Framework\Struct\ArrayStruct;
  6. use Shopware\Core\System\SalesChannel\Event\SalesChannelContextCreatedEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. class AddAffiliateInformationToSalesChannelContext implements EventSubscriberInterface
  10. {
  11.     public function __construct(private readonly RequestStack $requestStack)
  12.     {
  13.     }
  14.     public static function getSubscribedEvents(): array
  15.     {
  16.         return [
  17.             SalesChannelContextCreatedEvent::class => 'addAffiliateInformation',
  18.         ];
  19.     }
  20.     public function addAffiliateInformation(SalesChannelContextCreatedEvent $event): void
  21.     {
  22.         $salesChannelContext $event->getSalesChannelContext();
  23.         $affiliateInformation = new ArrayStruct([], 'affiliateInformation');
  24.         try {
  25.             $session $this->requestStack->getSession();
  26.             $request $this->requestStack->getCurrentRequest();
  27.             $affiliateCode $request->get(OrderService::AFFILIATE_CODE_KEY$session->get(OrderService::AFFILIATE_CODE_KEY));
  28.             if ($affiliateCode) {
  29.                 $affiliateInformation->set(OrderService::AFFILIATE_CODE_KEY$affiliateCode);
  30.             }
  31.             $campaignCode $request->get(OrderService::CAMPAIGN_CODE_KEY$session->get(OrderService::CAMPAIGN_CODE_KEY));
  32.             if ($campaignCode) {
  33.                 $affiliateInformation->set(OrderService::CAMPAIGN_CODE_KEY$campaignCode);
  34.             }
  35.         } catch (\Symfony\Component\HttpFoundation\Exception\SessionNotFoundException $exception) {
  36.             // If this is run in CLI context, there is no session
  37.         }
  38.         /** @noinspection PhpDeprecationInspection */
  39.         $salesChannelContext->addExtension('affiliateInformation'$affiliateInformation);
  40.     }
  41. }