<?php
declare(strict_types=1);
namespace WbfkExtensions\Subscriber;
use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
use Shopware\Core\System\StateMachine\Loader\InitialStateIdLoader;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use WbfkExtensions\Core\Checkout\Document\InvoiceStates;
class SetInitialInvoiceStateOnCartConversion implements EventSubscriberInterface
{
public function __construct(private readonly InitialStateIdLoader $initialStateIdLoader)
{
}
public static function getSubscribedEvents(): array
{
return [
CartConvertedEvent::class => 'setInitialInvoiceState',
];
}
public function setInitialInvoiceState(CartConvertedEvent $event): void
{
$convertedCart = $event->getConvertedCart();
$cart = $event->getCart();
if ($cart->getName() === 'recalculation' || isset($convertedCart['extensions']['wbfkOrderExtension']['invoiceStateId'])) {
return;
}
$convertedCart['extensions']['wbfkOrderExtension']['invoiceStateId'] = $this->initialStateIdLoader->get(InvoiceStates::STATE_MACHINE);
$event->setConvertedCart($convertedCart);
}
}