custom/plugins/MadMykey/src/Subscriber/CartSubscriber.php line 27

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Madco\Mykey\Subscriber;
  4. use Madco\Mykey\Constants;
  5. use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. class CartSubscriber implements EventSubscriberInterface
  9. {
  10.     public function __construct(
  11.         private RequestStack $requestStack
  12.     )
  13.     {
  14.     }
  15.     public static function getSubscribedEvents(): array
  16.     {
  17.         return [
  18.             CartConvertedEvent::class => 'addCartTokenToOrderData',
  19.         ];
  20.     }
  21.     public function addCartTokenToOrderData(CartConvertedEvent $event): void
  22.     {
  23.         $request $this->requestStack->getMainRequest();
  24.         $mykeyTracer null;
  25.         if ($request) {
  26.             $mykeyTracer = (string) $request->getSession()->get(Constants::MYKEY_TRACER_KEY);
  27.         }
  28.         $orderData $event->getConvertedCart();
  29.         $customFields $orderData['customFields'] ?? [];
  30.         $customFields[Constants::MYKEY_TRACER_KEY] = $mykeyTracer;
  31.         $orderData['customFields'] = $customFields;
  32.         $event->setConvertedCart($orderData);
  33.     }
  34. }