custom/plugins/WbfkExtensions/src/Subscriber/SetInitialInvoiceStateOnCartConversion.php line 27

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace WbfkExtensions\Subscriber;
  4. use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
  5. use Shopware\Core\System\StateMachine\Loader\InitialStateIdLoader;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use WbfkExtensions\Core\Checkout\Document\InvoiceStates;
  8. class SetInitialInvoiceStateOnCartConversion implements EventSubscriberInterface
  9. {
  10.     public function __construct(private readonly InitialStateIdLoader $initialStateIdLoader)
  11.     {
  12.     }
  13.     public static function getSubscribedEvents(): array
  14.     {
  15.         return [
  16.             CartConvertedEvent::class => 'setInitialInvoiceState',
  17.         ];
  18.     }
  19.     public function setInitialInvoiceState(CartConvertedEvent $event): void
  20.     {
  21.         $convertedCart $event->getConvertedCart();
  22.         $cart $event->getCart();
  23.         if ($cart->getName() === 'recalculation' || isset($convertedCart['extensions']['wbfkOrderExtension']['invoiceStateId'])) {
  24.             return;
  25.         }
  26.         $convertedCart['extensions']['wbfkOrderExtension']['invoiceStateId'] = $this->initialStateIdLoader->get(InvoiceStates::STATE_MACHINE);
  27.         $event->setConvertedCart($convertedCart);
  28.     }
  29. }