<?php
declare(strict_types=1);
namespace Wbfk\MultipleDeliveries\Subscriber;
use Shopware\Core\Checkout\Cart\Delivery\Struct\Delivery;
use Shopware\Core\Checkout\Cart\Order\IdStruct;
use Shopware\Core\Checkout\Cart\Order\OrderConvertedEvent;
use Shopware\Core\Checkout\Cart\Order\OrderConverter;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class OrderConvertedEventSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
OrderConvertedEvent::class => 'addOrderDeliveryStateToCartDelivery',
];
}
public function addOrderDeliveryStateToCartDelivery(OrderConvertedEvent $event): void
{
/*
* Attach state id of original deliveries to cart deliveries.
* This will be used to restore order delivery state after cart is converted to order.
*/
$event->getConvertedCart()->getDeliveries()->map(function (Delivery $delivery) use ($event) {
if (!$delivery->hasExtension(OrderConverter::ORIGINAL_ID)) {
return;
}
/** @var ?IdStruct $originalId */
$originalIdStruct = $delivery->getExtension(OrderConverter::ORIGINAL_ID);
if (!($originalIdStruct instanceof IdStruct)) {
return;
}
$originalDelivery = $event->getOrder()->getDeliveries()->get($originalIdStruct->getId());
/** @noinspection PhpDeprecationInspection */
$delivery->addExtension('originalStateId', new IdStruct($originalDelivery->getStateId()));
});
}
}