custom/plugins/WbfkMultipleDeliveries/src/Subscriber/CartConvertedSubscriber.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\CartConvertedEvent;
  6. use Shopware\Core\Checkout\Cart\Order\IdStruct;
  7. use Shopware\Core\Checkout\Cart\Order\OrderConverter;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class CartConvertedSubscriber implements EventSubscriberInterface
  10. {
  11.     public static function getSubscribedEvents(): array
  12.     {
  13.         return [
  14.             CartConvertedEvent::class => 'restoreOrderDeliveryState',
  15.         ];
  16.     }
  17.     public function restoreOrderDeliveryState(CartConvertedEvent $event): void
  18.     {
  19.         $convertedCart $event->getConvertedCart();
  20.         $cart $event->getCart();
  21.         foreach ($cart->getDeliveries() as $delivery) {
  22.             /** @var Delivery $delivery */
  23.             /** @var ?IdStruct $originalStateIdStruct */
  24.             $originalStateIdStruct $delivery->getExtension('originalStateId');
  25.             if (!$originalStateIdStruct) {
  26.                 continue;
  27.             }
  28.             /** @var ?IdStruct $originalIdStruct */
  29.             $originalIdStruct $delivery->getExtension(OrderConverter::ORIGINAL_ID);
  30.             if (!($originalIdStruct instanceof IdStruct)) {
  31.                 continue;
  32.             }
  33.             foreach ($convertedCart['deliveries'] as $key => $convertedDelivery) {
  34.                 if ($convertedDelivery['id'] == $originalIdStruct->getId()) {
  35.                     $convertedCart['deliveries'][$key]['stateId'] = $originalStateIdStruct->getId();
  36.                     break;
  37.                 }
  38.             }
  39.         }
  40.         $event->setConvertedCart($convertedCart);
  41.     }
  42. }