custom/plugins/AbmAdjustments/src/Subscriber/OrderTriggerSubscriber.php line 31

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace AbmAdjustments\Subscriber;
  3. use Shopware\Core\Framework\Event\BusinessEventCollector;
  4. use Shopware\Core\Framework\Event\BusinessEventCollectorEvent;
  5. use AbmAdjustments\Flow\OrderDeliveryShippedEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Shopware\Core\Framework\Event\MailRecipientStruct;
  8. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  10. class OrderTriggerSubscriber implements EventSubscriberInterface
  11. {
  12.     private BusinessEventCollector $businessEventCollector;
  13.     private EntityRepository $orderRepository;
  14.     public function __construct(BusinessEventCollector $businessEventCollectorEntityRepository $orderRepository) {
  15.         $this->businessEventCollector $businessEventCollector;
  16.         $this->orderRepository $orderRepository;
  17.     }
  18.     public static function getSubscribedEvents()
  19.     {
  20.         return [
  21.             BusinessEventCollectorEvent::NAME => ['onAddOrderDeliveryShippedEvent'1000],
  22.             CheckoutOrderPlacedEvent::class  => 'onOrderPlaced'
  23.         ];
  24.     }
  25.     public function onAddOrderDeliveryShippedEvent(BusinessEventCollectorEvent $event): void
  26.     {
  27.         $collection $event->getCollection();
  28.         $definition $this->businessEventCollector->define(OrderDeliveryShippedEvent::class);
  29.         if (!$definition) {
  30.             return;
  31.         }
  32.         $collection->set($definition->getName(), $definition);
  33.     }
  34.     public function onOrderPlaced(CheckoutOrderPlacedEvent $event): void
  35.     {
  36.         $latestReleaseDate null;
  37.         $hasPreorderItem false;
  38.         foreach ($event->getOrder()->getLineItems() as $lineItem) {
  39.             $payload $lineItem->getPayload();
  40.             if (isset($payload['releaseDate'])) {
  41.                 $releaseDate = new \DateTime($payload['releaseDate']);
  42.                 $now = new \DateTime();
  43.                 
  44.                 if ($releaseDate $now) {
  45.                     $hasPreorderItem true;
  46.                     if ($latestReleaseDate === null || $releaseDate $latestReleaseDate) {
  47.                         $latestReleaseDate $releaseDate;
  48.                     }
  49.                 }
  50.             }
  51.         }
  52.         if ($hasPreorderItem && $latestReleaseDate !== null) {
  53.             $formattedDate $latestReleaseDate->format('d.m.Y');
  54.             $existingComment $event->getOrder()->getCustomerComment() ?? '';
  55.             $newComment trim($existingComment "\n" ' Vorbestellung mit Datum: ' $formattedDate);
  56.             $this->orderRepository->update([
  57.                 [
  58.                     'id' => $event->getOrderId(),
  59.                     'customerComment' => $newComment
  60.                 ]
  61.             ], $event->getContext());
  62.         }
  63.     }
  64. }