custom/static-plugins/AbasConnector/src/Subscribers/Order/TransformAddressSubcriber.php line 44

Open in your IDE?
  1. <?php
  2. namespace Abas\AbasConnector\Subscribers\Order;
  3. use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
  4. use Shopware\Core\Checkout\Cart\Order\Transformer\DeliveryTransformer;
  5. use Shopware\Core\Checkout\Cart\Order\Transformer\LineItemTransformer;
  6. use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryStates;
  7. use Shopware\Core\System\StateMachine\StateMachineRegistry;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use UnexpectedValueException;
  10. class TransformAddressSubcriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var StateMachineRegistry
  14.      */
  15.     private $stateMachineRegistry;
  16.     /**
  17.      * @param StateMachineRegistry $stateMachineRegistry
  18.      */
  19.     public function __construct(StateMachineRegistry $stateMachineRegistry)
  20.     {
  21.         $this->stateMachineRegistry $stateMachineRegistry;
  22.     }
  23.     /**
  24.      * @inheritDoc
  25.      */
  26.     public static function getSubscribedEvents()
  27.     {
  28.         return [
  29.             CartConvertedEvent::class => 'addAddressCustomFields'
  30.         ];
  31.     }
  32.     /**
  33.      * @param CartConvertedEvent $event
  34.      *
  35.      * @return void
  36.      * @throws UnexpectedValueException
  37.      */
  38.     public function addAddressCustomFields(CartConvertedEvent $event): void
  39.     {
  40.         $cart $event->getCart();
  41.         $data $event->getConvertedCart();
  42.         $conversionContext $event->getConversionContext();
  43.         $convertedLineItems LineItemTransformer::transformCollection($cart->getLineItems());
  44.         $shippingAddresses = [];
  45.         if ($conversionContext->shouldIncludeDeliveries()) {
  46.             $shippingAddresses AddressTransformer::transformCollection($cart->getDeliveries()->getAddresses(), true);
  47.             $context $event->getContext();
  48.             $data['deliveries'] = DeliveryTransformer::transformCollection(
  49.                 $cart->getDeliveries(),
  50.                 $convertedLineItems,
  51.                 $this->stateMachineRegistry->getInitialState(
  52.                     OrderDeliveryStates::STATE_MACHINE,
  53.                     $context
  54.                 )->getId(),
  55.                 $context,
  56.                 $shippingAddresses
  57.             );
  58.         }
  59.         if ($conversionContext->shouldIncludeBillingAddress()) {
  60.             $customer $event->getSalesChannelContext()->getCustomer();
  61.             if ($customer === null) {
  62.                 throw new UnexpectedValueException('customer is not available.');
  63.             }
  64.             $activeBillingAddress $customer->getActiveBillingAddress();
  65.             if ($activeBillingAddress === null) {
  66.                 throw new UnexpectedValueException('active billing address is not available.');
  67.             }
  68.             $customerAddressId $activeBillingAddress->getId();
  69.             if (array_key_exists($customerAddressId$shippingAddresses)) {
  70.                 $billingAddressId $shippingAddresses[$customerAddressId]['id'];
  71.             } else {
  72.                 $billingAddress AddressTransformer::transform($activeBillingAddress);
  73.                 $data['addresses'] = [$billingAddress];
  74.                 $billingAddressId $billingAddress['id'];
  75.             }
  76.             $data['billingAddressId'] = $billingAddressId;
  77.         }
  78.         $data['lineItems'] = array_values($convertedLineItems);
  79.         $event->setConvertedCart($data);
  80.     }
  81. }