custom/plugins/WbfkExtensions/src/Subscriber/RecalculateProductSupplierDeliveryTimesOnCheckout.php line 28

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace WbfkExtensions\Subscriber;
  4. use Enqueue\MessengerAdapter\EnvelopeItem\TransportConfiguration;
  5. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  6. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  7. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Messenger\MessageBusInterface;
  10. use WbfkExtensions\MessageQueue\Message\RecalculateProductSupplierDeliveryTimes;
  11. class RecalculateProductSupplierDeliveryTimesOnCheckout implements EventSubscriberInterface
  12. {
  13.     public function __construct(private readonly MessageBusInterface $messageBus)
  14.     {
  15.     }
  16.     public static function getSubscribedEvents(): array
  17.     {
  18.         return [
  19.             CheckoutOrderPlacedEvent::class => 'onOrderPlaced',
  20.         ];
  21.     }
  22.     public function onOrderPlaced(CheckoutOrderPlacedEvent $event): void
  23.     {
  24.         $lineItems $event->getOrder()->getLineItems();
  25.         if ($lineItems === null || $lineItems->count() <= 0) {
  26.             return;
  27.         }
  28.         $productIds $lineItems->fmap(static function (OrderLineItemEntity $orderLineItem) {
  29.             if ($orderLineItem->getType() !== LineItem::PRODUCT_LINE_ITEM_TYPE) {
  30.                 return null;
  31.             }
  32.             return $orderLineItem->getReferencedId();
  33.         });
  34.         $this->messageBus->dispatch(
  35.             new RecalculateProductSupplierDeliveryTimes(
  36.                 $productIds,
  37.                 "checkout order placed. order id: {$event->getOrder()->getId()}"
  38.             ),
  39.             [
  40.                 new TransportConfiguration([
  41.                     'metadata' => [
  42.                         'priority' => -1000// priority less than mails (0)
  43.                     ],
  44.                 ]),
  45.             ]
  46.         );
  47.     }
  48. }