<?php
declare(strict_types=1);
namespace Madco\Mykey\Subscriber;
use Madco\Mykey\Constants;
use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class CartSubscriber implements EventSubscriberInterface
{
public function __construct(
private RequestStack $requestStack
)
{
}
public static function getSubscribedEvents(): array
{
return [
CartConvertedEvent::class => 'addCartTokenToOrderData',
];
}
public function addCartTokenToOrderData(CartConvertedEvent $event): void
{
$request = $this->requestStack->getMainRequest();
$mykeyTracer = null;
if ($request) {
$mykeyTracer = (string) $request->getSession()->get(Constants::MYKEY_TRACER_KEY);
}
$orderData = $event->getConvertedCart();
$customFields = $orderData['customFields'] ?? [];
$customFields[Constants::MYKEY_TRACER_KEY] = $mykeyTracer;
$orderData['customFields'] = $customFields;
$event->setConvertedCart($orderData);
}
}