<?php declare(strict_types=1);
namespace TigerMedia\General\TigerRequestDeliveryDate\Subscriber;
use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class CartConvertedSubscriber implements EventSubscriberInterface
{
private RequestStack $requestStack;
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
public static function getSubscribedEvents()
{
return [
CartConvertedEvent::class => 'orderWritten',
];
}
public function orderWritten(CartConvertedEvent $event): void
{
$orderData = $event->getConvertedCart();
$orderCustomFields = $orderData['customFields'] ?? [];
$requestedDeliveryDate = $this->requestStack->getCurrentRequest()->request->get('requestDeliveryDate');
if ($requestedDeliveryDate) {
$orderCustomFields['tigermediarequestdelivery'] =
$requestedDeliveryDate;
}
$orderData['customFields'] = $orderCustomFields;
$event->setConvertedCart($orderData);
}
}