platform/src/Storefront/Framework/AffiliateTracking/AffiliateTrackingListener.php line 26

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Framework\AffiliateTracking;
  3. use Shopware\Core\Checkout\Order\SalesChannel\OrderService;
  4. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  5. use Shopware\Core\Framework\Routing\KernelListenerPriorities;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. class AffiliateTrackingListener implements EventSubscriberInterface
  10. {
  11.     public const AFFILIATE_CODE_KEY OrderService::AFFILIATE_CODE_KEY;
  12.     public const CAMPAIGN_CODE_KEY OrderService::CAMPAIGN_CODE_KEY;
  13.     public static function getSubscribedEvents(): array
  14.     {
  15.         return [
  16.             KernelEvents::CONTROLLER => [
  17.                 ['checkAffiliateTracking'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_SCOPE_VALIDATE_POST],
  18.             ],
  19.         ];
  20.     }
  21.     public function checkAffiliateTracking(ControllerEvent $event): void
  22.     {
  23.         $request $event->getRequest();
  24.         /** @var RouteScope|null $routeScope */
  25.         $routeScope $request->attributes->get('_routeScope');
  26.         // Only process storefront routes
  27.         if ($routeScope && !\in_array('storefront'$routeScope->getScopes(), true)) {
  28.             return;
  29.         }
  30.         $session $request->getSession();
  31.         $affiliateCode $request->query->get(self::AFFILIATE_CODE_KEY);
  32.         $campaignCode $request->query->get(self::CAMPAIGN_CODE_KEY);
  33.         if ($affiliateCode) {
  34.             $session->set(self::AFFILIATE_CODE_KEY$affiliateCode);
  35.         }
  36.         if ($campaignCode) {
  37.             $session->set(self::CAMPAIGN_CODE_KEY$campaignCode);
  38.         }
  39.     }
  40. }