custom/plugins/DtgsDeliveryDateSw6/src/Subscriber/OrderSubscriber.php line 51

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Dtgs\DeliveryDate\Subscriber;
  3. use DateTime;
  4. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  5. use Shopware\Core\Checkout\Order\OrderEntity;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\System\SystemConfig\SystemConfigService;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\Session\Session;
  11. class OrderSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var SystemConfigService
  15.      */
  16.     private $systemConfigService;
  17.     /**
  18.      * @var EntityRepositoryInterface
  19.      */
  20.     private $orderRepository;
  21.     /**
  22.      * @var Session
  23.      */
  24.     private $session;
  25.     public function __construct(SystemConfigService $systemConfigService,
  26.                                 EntityRepositoryInterface $orderRepository,
  27.                                 Session $session)
  28.     {
  29.         $this->systemConfigService $systemConfigService;
  30.         $this->orderRepository $orderRepository;
  31.         $this->session $session;
  32.     }
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return[
  36.             CheckoutOrderPlacedEvent::class => 'onOrderPlaced',
  37.         ];
  38.     }
  39.     /**
  40.      * Event für alle Seiten
  41.      *
  42.      * @param CheckoutOrderPlacedEvent $event
  43.      * @throws \Exception
  44.      */
  45.     public function onOrderPlaced(CheckoutOrderPlacedEvent $event)
  46.     {
  47.         $deliveryDateConfig $this->systemConfigService->get('DtgsDeliveryDateSw6.config'$event->getSalesChannelId());
  48.         $deliveryDateFormat = ($deliveryDateConfig['deliveryDateEnableTimePicker']) ? 'd.m.Y H:i' 'd.m.Y';
  49.         $order $event->getOrder();
  50.         $orderId $order->getId();
  51.         $context $event->getContext();
  52.         $deliveryDate $this->session->get('deliverydate'null);
  53.         if($deliveryDate) {
  54.             $this->orderRepository->upsert([[
  55.                 'id' => $orderId,
  56.                 'customFields' => ['dtgs_delivery_date' => $deliveryDate]
  57.             ]], $context);
  58.             //Set as earliest shipping date
  59.             if(isset($deliveryDateConfig['deliveryDateSetAsEarliestDate']) && $deliveryDateConfig['deliveryDateSetAsEarliestDate']) {
  60.                 $this->setAsEarliestShippingDate($order$deliveryDate$deliveryDateFormat$context);
  61.             }
  62.         }
  63.         $existingCustomFields $event->getOrder()->getCustomFields();
  64.         if(!$existingCustomFields$existingCustomFields = [];
  65.         //add delivery date
  66.         $deliverydateCustomFields = [
  67.             'deliveryDate' => $deliveryDate
  68.         ];
  69.         $mergedCustomFields array_merge($existingCustomFields$deliverydateCustomFields);
  70.         $event->getOrder()->setCustomFields($mergedCustomFields);
  71.         //unset session key
  72.         $this->session->remove('deliverydate');
  73.     }
  74.     /**
  75.      * since V6.0.6
  76.      *
  77.      * @param OrderEntity $order
  78.      * @param $deliveryDate
  79.      * @param string $dateFormat
  80.      * @param Context $context
  81.      */
  82.     private function setAsEarliestShippingDate(OrderEntity $order$deliveryDatestring $dateFormatContext $context)
  83.     {
  84.         $delivery $order->getDeliveries()->first();
  85.         $this->orderRepository->upsert([[
  86.             'id' => $order->getId(),
  87.             'deliveries' => [
  88.                 [
  89.                     'id' => $delivery->getId(),
  90.                     'shippingDateEarliest' => DateTime::createFromFormat($dateFormat$deliveryDate)
  91.                 ]
  92.             ],
  93.         ]], $context);
  94.     }
  95. }