<?php declare(strict_types=1);namespace DonCarneTheme\Subscriber;use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;use Shopware\Core\Checkout\Order\OrderEntity;use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;use Shopware\Core\Framework\Context;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpFoundation\RequestStack;class OrderDeliveryDateSubscriber implements EventSubscriberInterface{ private EntityRepositoryInterface $orderRepository; private RequestStack $requestStack; public function __construct( EntityRepositoryInterface $orderRepository, RequestStack $requestStack ) { $this->orderRepository = $orderRepository; $this->requestStack = $requestStack; } public static function getSubscribedEvents(): array { return [ CheckoutOrderPlacedEvent::class => 'onOrderPlaced', ]; } public function onOrderPlaced(CheckoutOrderPlacedEvent $event): void { $order = $event->getOrder(); $context = $event->getContext(); $request = $this->requestStack->getCurrentRequest(); if (!$request) { return; } $session = $request->getSession(); $savedDeliveryDates = $session->get('selected_delivery_dates', []); if (empty($savedDeliveryDates)) { return; } // Get the shipping method ID from the order delivery $orderDeliveries = $order->getDeliveries(); if (!$orderDeliveries || $orderDeliveries->count() === 0) { return; } $delivery = $orderDeliveries->first(); $shippingMethod = $delivery->getShippingMethod(); if (!$shippingMethod) { return; } // Try to get the GraphQL ID from custom fields $customFields = $shippingMethod->getTranslation('customFields'); $gqlShippingMethodId = null; if (is_array($customFields) && isset($customFields['custom_shipping_method_graphql_id'])) { $gqlShippingMethodId = $customFields['custom_shipping_method_graphql_id']; } // Also try with the regular shipping method ID as fallback $shippingMethodId = $shippingMethod->getId(); // Check if we have a saved date for this shipping method $selectedDate = null; if ($gqlShippingMethodId && isset($savedDeliveryDates[$gqlShippingMethodId])) { $selectedDate = $savedDeliveryDates[$gqlShippingMethodId]; } elseif (isset($savedDeliveryDates[$shippingMethodId])) { $selectedDate = $savedDeliveryDates[$shippingMethodId]; } if (!$selectedDate) { return; } // Store the date in YYYY-MM-DD format $dateOnly = $this->formatDateOnly($selectedDate); // Update the order with the selected delivery date $this->orderRepository->update([ [ 'id' => $order->getId(), 'customFields' => [ 'expected_delivery_date' => $dateOnly ] ] ], Context::createDefaultContext()); } private function formatDateOnly(array $dateData): string { if (!isset($dateData['date'])) { return ''; } $date = new \DateTime($dateData['date']); return $date->format('Y-m-d'); }}