custom/plugins/theme/src/Subscriber/OrderDeliveryDateSubscriber.php line 32

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace DonCarneTheme\Subscriber;
  3. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  4. use Shopware\Core\Checkout\Order\OrderEntity;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\Context;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. class OrderDeliveryDateSubscriber implements EventSubscriberInterface
  10. {
  11.     private EntityRepositoryInterface $orderRepository;
  12.     private RequestStack $requestStack;
  13.     public function __construct(
  14.         EntityRepositoryInterface $orderRepository,
  15.         RequestStack $requestStack
  16.     ) {
  17.         $this->orderRepository $orderRepository;
  18.         $this->requestStack $requestStack;
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             CheckoutOrderPlacedEvent::class => 'onOrderPlaced',
  24.         ];
  25.     }
  26.     public function onOrderPlaced(CheckoutOrderPlacedEvent $event): void
  27.     {
  28.         $order $event->getOrder();
  29.         $context $event->getContext();
  30.         $request $this->requestStack->getCurrentRequest();
  31.         if (!$request) {
  32.             return;
  33.         }
  34.         $session $request->getSession();
  35.         $savedDeliveryDates $session->get('selected_delivery_dates', []);
  36.         if (empty($savedDeliveryDates)) {
  37.             return;
  38.         }
  39.         // Get the shipping method ID from the order delivery
  40.         $orderDeliveries $order->getDeliveries();
  41.         if (!$orderDeliveries || $orderDeliveries->count() === 0) {
  42.             return;
  43.         }
  44.         $delivery $orderDeliveries->first();
  45.         $shippingMethod $delivery->getShippingMethod();
  46.         if (!$shippingMethod) {
  47.             return;
  48.         }
  49.         // Try to get the GraphQL ID from custom fields
  50.         $customFields $shippingMethod->getTranslation('customFields');
  51.         $gqlShippingMethodId null;
  52.         if (is_array($customFields) && isset($customFields['custom_shipping_method_graphql_id'])) {
  53.             $gqlShippingMethodId $customFields['custom_shipping_method_graphql_id'];
  54.         }
  55.         // Also try with the regular shipping method ID as fallback
  56.         $shippingMethodId $shippingMethod->getId();
  57.         // Check if we have a saved date for this shipping method
  58.         $selectedDate null;
  59.         if ($gqlShippingMethodId && isset($savedDeliveryDates[$gqlShippingMethodId])) {
  60.             $selectedDate $savedDeliveryDates[$gqlShippingMethodId];
  61.         } elseif (isset($savedDeliveryDates[$shippingMethodId])) {
  62.             $selectedDate $savedDeliveryDates[$shippingMethodId];
  63.         }
  64.         if (!$selectedDate) {
  65.             return;
  66.         }
  67.         // Store the date in YYYY-MM-DD format
  68.         $dateOnly $this->formatDateOnly($selectedDate);
  69.         // Update the order with the selected delivery date
  70.         $this->orderRepository->update([
  71.             [
  72.                 'id' => $order->getId(),
  73.                 'customFields' => [
  74.                     'expected_delivery_date' => $dateOnly
  75.                 ]
  76.             ]
  77.         ], Context::createDefaultContext());
  78.     }
  79.     private function formatDateOnly(array $dateData): string
  80.     {
  81.         if (!isset($dateData['date'])) {
  82.             return '';
  83.         }
  84.         $date = new \DateTime($dateData['date']);
  85.         return $date->format('Y-m-d');
  86.     }
  87. }