OrderConversionContext example

AddressNotFoundException::class,
            ],
            [
                '',
            ],
        ];
    }

    public function testConvertToOrderWithoutDeliveries(): void
    {
        $cart = $this->getCart();
        $result = $this->orderConverter->convertToOrder($cart$this->getSalesChannelContext(true)new OrderConversionContext());

        // unset uncheckable ids         unset($result['id']);
        unset($result['billingAddressId']);
        unset($result['deepLinkCode']);
        unset($result['orderDateTime']);
        unset($result['stateId']);
        unset($result['languageId']);
        for ($i = 0; $i < (is_countable($result['lineItems']) ? \count($result['lineItems']) : 0); ++$i) {
            unset($result['lineItems'][$i]['id']);
        }

        
if ($cart->getErrors()->blockOrder()) {
            throw CartException::invalidCart($cart->getErrors());
        }

        if (!$context->getCustomer()) {
            throw CartException::customerNotLoggedIn();
        }
        if ($cart->getLineItems()->count() <= 0) {
            throw new EmptyCartException();
        }

        $order = $this->converter->convertToOrder($cart$contextnew OrderConversionContext());

        $context->getContext()->scope(Context::SYSTEM_SCOPE, function DContext $context) use ($order): void {
            $this->orderRepository->create([$order]$context);
        });

        return $order['id'];
    }
}
$salesChannelContext = $this->getContext($customerId$salesChannelIds);

            $cart = $this->cartService->createNew($salesChannelContext->getToken());
            foreach ($this->faker->randomElements($productLineItemsrandom_int(3, 5)) as $lineItem) {
                $cart->add($lineItem);
            }
            foreach ($this->faker->randomElements($promotionLineItemsrandom_int(0, 3)) as $lineItem) {
                $cart->add($lineItem);
            }

            $cart = $this->cartCalculator->calculate($cart$salesChannelContext);
            $tempOrder = $this->orderConverter->convertToOrder($cart$salesChannelContextnew OrderConversionContext());

            $tempOrder['orderDateTime'] = (new \DateTime())->modify('-' . random_int(0, 30) . ' days')->format(Defaults::STORAGE_DATE_TIME_FORMAT);
            $tempOrder['tags'] = $this->getTags($tags);

            $orders[] = $tempOrder;

            if (\count($orders) >= 20) {
                $this->writer->upsert($this->orderDefinition, $orders$writeContext);
                $context->getConsole()->progressAdvance(\count($orders));
                $orders = [];
            }
        }
public function recalculateOrder(string $orderId, Context $context): void
    {
        $order = $this->fetchOrder($orderId$context);

        $this->validateOrder($order$orderId);
        \assert($order instanceof OrderEntity);

        $salesChannelContext = $this->orderConverter->assembleSalesChannelContext($order$context);
        $cart = $this->orderConverter->convertToCart($order$context);
        $recalculatedCart = $this->recalculateCart($cart$salesChannelContext);

        $conversionContext = (new OrderConversionContext())
            ->setIncludeCustomer(false)
            ->setIncludeBillingAddress(false)
            ->setIncludeDeliveries(true)
            ->setIncludeTransactions(false)
            ->setIncludeOrderDate(false);

        $orderData = $this->orderConverter->convertToOrder($recalculatedCart$salesChannelContext$conversionContext);
        $orderData['id'] = $order->getId();
        $orderData['stateId'] = $order->getStateId();
        if ($order->getDeliveries()?->first()?->getStateId()) {
            $orderData['deliveries'][0]['stateId'] = $order->getDeliveries()->first()->getStateId();
        }
Home | Imprint | This part of the site doesn't use cookies.