custom/plugins/WbfkMultipleDeliveries/src/Subscriber/OrderConvertedEventSubscriber.php line 22

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Wbfk\MultipleDeliveries\Subscriber;
  4. use Shopware\Core\Checkout\Cart\Delivery\Struct\Delivery;
  5. use Shopware\Core\Checkout\Cart\Order\IdStruct;
  6. use Shopware\Core\Checkout\Cart\Order\OrderConvertedEvent;
  7. use Shopware\Core\Checkout\Cart\Order\OrderConverter;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class OrderConvertedEventSubscriber implements EventSubscriberInterface
  10. {
  11.     public static function getSubscribedEvents(): array
  12.     {
  13.         return [
  14.             OrderConvertedEvent::class => 'addOrderDeliveryStateToCartDelivery',
  15.         ];
  16.     }
  17.     public function addOrderDeliveryStateToCartDelivery(OrderConvertedEvent $event): void
  18.     {
  19.         /*
  20.          * Attach state id of original deliveries to cart deliveries.
  21.          * This will be used to restore order delivery state after cart is converted to order.
  22.          */
  23.         $event->getConvertedCart()->getDeliveries()->map(function (Delivery $delivery) use ($event) {
  24.             if (!$delivery->hasExtension(OrderConverter::ORIGINAL_ID)) {
  25.                 return;
  26.             }
  27.             /** @var ?IdStruct $originalId */
  28.             $originalIdStruct $delivery->getExtension(OrderConverter::ORIGINAL_ID);
  29.             if (!($originalIdStruct instanceof IdStruct)) {
  30.                 return;
  31.             }
  32.             $originalDelivery $event->getOrder()->getDeliveries()->get($originalIdStruct->getId());
  33.             /** @noinspection PhpDeprecationInspection */
  34.             $delivery->addExtension('originalStateId', new IdStruct($originalDelivery->getStateId()));
  35.         });
  36.     }
  37. }