convertToCart example

$salesChannelContext->getContext()->addState(Context::SKIP_TRIGGER_FLOW);
        }

        if ($order->getItemRounding() !== null) {
            $salesChannelContext->setItemRounding($order->getItemRounding());
        }

        if ($order->getTotalRounding() !== null) {
            $salesChannelContext->setTotalRounding($order->getTotalRounding());
        }

        $cart = $this->orderConverter->convertToCart($order$salesChannelContext->getContext());
        $this->cartRuleLoader->loadByCart(
            $salesChannelContext,
            $cart,
            new CartBehavior($salesChannelContext->getPermissions()),
            true
        );

        return $salesChannelContext;
    }

    /** * @param array<string> $overrideOptions * * @throws Exception */

    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();
        

            [
                CartException::class,
                false,
                false,
            ],
        ];
    }

    public function testConvertToCart(): void
    {
        $result = $this->orderConverter->convertToCart($this->getOrder(), Context::createDefaultContext());
        $result = \json_encode($result, \JSON_THROW_ON_ERROR);
        static::assertIsString($result);
        $result = \json_decode($result, true, 512, \JSON_THROW_ON_ERROR);
        static::assertNotFalse($result);

        // unset uncheckable ids         unset($result['extensions']['originalId']);
        unset($result['token']);
        for ($i = 0; $i < (is_countable($result['lineItems']) ? \count($result['lineItems']) : 0); ++$i) {
            unset($result['lineItems'][$i]['extensions']['originalId']);
            unset($result['lineItems'][$i]['uniqueIdentifier']);
        }
->addAssociation('deliveries.positions.orderLineItem')
            ->addAssociation('deliveries.shippingOrderAddress.country')
            ->addAssociation('deliveries.shippingOrderAddress.countryState');

        /** @var OrderEntity $order */
        $order = $this->getContainer()->get('order.repository')
            ->search($criteria$context->getContext())
            ->get($orderId);
        static::assertNotNull($order);

        $cart = $this->getContainer()->get(OrderConverter::class)
            ->convertToCart($order$context->getContext());

        $context->setPermissions([
            PromotionProcessor::SKIP_PROMOTION => true,
        ]);

        $cart = $this->cartService->recalculate($cart$context);

        static::assertCount(2, $cart->getLineItems());
        $promotion = $cart->getLineItems()->filterType(LineItem::PROMOTION_LINE_ITEM_TYPE);
        static::assertCount(1, $promotion, 'Promotion was removed');
    }

    

        $this->scopes = [];
    }

    public function build(OrderEntity $order, Context $context): FlowRuleScope
    {
        if (\array_key_exists($order->getId()$this->scopes)) {
            return $this->scopes[$order->getId()];
        }

        $context = $this->orderConverter->assembleSalesChannelContext($order$context);
        $cart = $this->orderConverter->convertToCart($order$context->getContext());
        $behavior = new CartBehavior($context->getPermissions());

        foreach ($this->collectors as $collector) {
            $collector->collect($cart->getData()$cart$context$behavior);
        }

        $cart->setDeliveries(
            $this->deliveryBuilder->build($cart$cart->getData()$context$behavior)
        );

        return $this->scopes[$order->getId()] = new FlowRuleScope($order$cart$context);
    }
/** @var OrderEntity|null $order */
        $order = $this->orderRepository->search($criteria$context)->get($orderId);

        if (!$order) {
            if (!Feature::isActive('v6.6.0.0')) {
                throw new InvalidOrderException($orderId);
            }

            throw CartException::orderNotFound($orderId);
        }

        $convertedCart = $this->orderConverter->convertToCart($order$context);

        $this->cartPersister->save(
            $convertedCart,
            $this->orderConverter->assembleSalesChannelContext($order$context)
        );

        return new JsonResponse(['token' => $convertedCart->getToken()]);
    }
}

        }

        return false;
    }

    /** * @return string[] */
    private function getOrderRules(OrderEntity $order, SalesChannelContext $salesChannelContext): array
    {
        $convertedCart = $this->orderConverter->convertToCart($order$salesChannelContext->getContext());
        $ruleIds = $this->cartRuleLoader->loadByCart(
            $salesChannelContext,
            $convertedCart,
            new CartBehavior($salesChannelContext->getPermissions())
        )->getMatchingRules()->getIds();

        return array_values($ruleIds);
    }

    private function loadOrder(string $orderId, SalesChannelContext $context): OrderEntity
    {
        
        $idx = 0;
        foreach ($order->getNestedLineItems() as $lineItem) {
            if ($idx === 0) {
                static::assertEquals($parentProductId$lineItem->getReferencedId());
            } else {
                static::assertEquals($rootProductId$lineItem->getReferencedId());
            }
            ++$idx;
        }

        $convertedCart = $this->getContainer()->get(OrderConverter::class)
            ->convertToCart($order$this->context);

        // check token         static::assertNotEquals($cart->getToken()$convertedCart->getToken());
        static::assertTrue(Uuid::isValid($convertedCart->getToken()));

        // check lineItem sorting         $idx = 0;
        foreach ($convertedCart->getLineItems() as $lineItem) {
            if ($idx === 0) {
                static::assertEquals($parentProductId$lineItem->getId());
            } else {
                
Home | Imprint | This part of the site doesn't use cookies.