recalculate example

->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 test verifies that our cart services * does also correctly remove the matching code * within our extension, if existing AND a fixed discount has been added that * is discounting TWO products. * We add two products and promotion code, then we grab one promotion discount * line item id and remove it. * After that we verify that our code array is empty in our extension (both discounts on the * two products are removed). * * @group promotions */
 SalesChannelContext {
        if (!$customerContext->getDomainId()) {
            $customerContext->setDomainId($currentContext->getDomainId());
        }

        $guestCart = $this->cartService->getCart($token$currentContext);
        $customerCart = $this->cartService->getCart($customerContext->getToken()$customerContext);

        if ($guestCart->getLineItems()->count() > 0) {
            $restoredCart = $this->mergeCart($customerCart$guestCart$customerContext);
        } else {
            $restoredCart = $this->cartService->recalculate($customerCart$customerContext);
        }

        $restoredCart->addErrors(...array_values($guestCart->getErrors()->getPersistent()->getElements()));

        $this->deleteGuestContext($currentContext$customerId);

        $errors = $restoredCart->getErrors();
        $result = $this->cartRuleLoader->loadByToken($customerContext$restoredCart->getToken());

        $cartWithErrors = $result->getCart();
        $cartWithErrors->setErrors($errors);
        
protected CartService $cartService,
        protected SalesChannelContextPersister $contextPersister
    ) {
    }

    public function updateShippingCosts(CalculatedPrice $calculatedPrice, SalesChannelContext $context): Cart
    {
        $cart = $this->cartService->getCart($context->getToken()$context);

        $cart->addExtension(DeliveryProcessor::MANUAL_SHIPPING_COSTS, $calculatedPrice);

        return $this->cartService->recalculate($cart$context);
    }

    public function addPermission(string $token, string $permission, string $salesChannelId): void
    {
        $payload = $this->contextPersister->load($token$salesChannelId);

        if (!\array_key_exists(SalesChannelContextService::PERMISSIONS, $payload)) {
            $payload[SalesChannelContextService::PERMISSIONS] = [];
        }

        $payload[SalesChannelContextService::PERMISSIONS][$permission] = true;
        
$this->getContainer()->get('product.repository')
            ->create($products, Context::createDefaultContext());

        $cart = $this->getContainer()->get(Processor::class)->process($cart$this->salesChannelContext, new CartBehavior());

        return $cart;
    }

    private function persistCart(Cart $cart): string
    {
        $cart = $this->getContainer()->get(CartService::class)->recalculate($cart$this->salesChannelContext);
        $orderId = $this->getContainer()->get(OrderPersister::class)->persist($cart$this->salesChannelContext);

        return $orderId;
    }

    private function createCustomer(string $paymentMethodId): string
    {
        $customerId = Uuid::randomHex();
        $addressId = Uuid::randomHex();

        $customer = [
            
$this->getContainer()->get('product.repository')
            ->create($products, Context::createDefaultContext());

        $cart = $this->getContainer()->get(Processor::class)->process($cart$this->salesChannelContext, new CartBehavior());

        return $cart;
    }

    private function persistCart(Cart $cart): string
    {
        $cart = $this->getContainer()->get(CartService::class)->recalculate($cart$this->salesChannelContext);

        return $this->getContainer()->get(OrderPersister::class)->persist($cart$this->salesChannelContext);
    }

    private function createCustomer(string $paymentMethodId): string
    {
        $customerId = Uuid::randomHex();
        $addressId = Uuid::randomHex();

        $customer = [
            'id' => $customerId,
            
Home | Imprint | This part of the site doesn't use cookies.