removeItems example

$cart = $this->itemUpdateRoute->change($request$cart$context)->getCart();

        return $this->cart[$cart->getToken()] = $cart;
    }

    /** * @throws CartException */
    public function remove(Cart $cart, string $identifier, SalesChannelContext $context): Cart
    {
        return $this->removeItems($cart[$identifier]$context);
    }

    /** * @param string[] $ids * * @throws CartException */
    public function removeItems(Cart $cart, array $ids, SalesChannelContext $context): Cart
    {
        $request = new Request();
        $request->request->set('ids', $ids);

        
foreach ($products as $product) {
            $this->addTaxDataToSalesChannel($context$product['tax']);

            $lineItems[] = (new ProductLineItemFactory(new PriceDefinitionFactory()))->create(['id' => $product['id'], 'referencedId' => $product['id']]$context);
        }

        $cart = $cartService->getCart($context->getToken()$context);
        $cart = $cartService->add($cart$lineItems$context);

        static::assertCount(3, $cart->getLineItems());

        $cart = $cartService->removeItems($cart[
            $productId1,
            $productId2,
        ]$context);

        static::assertCount(1, $cart->getLineItems());

        $remainingLineItem = $cart->getLineItems()->get($productId3);
        static::assertInstanceOf(LineItem::class$remainingLineItem);
        static::assertEquals($productId3$remainingLineItem->getReferencedId());
    }

    
$id2 = Uuid::randomHex();
        $ids = [$id1$id2];

        $this->cartItemRemoveRoute->expects(static::once())
            ->method('remove')
            ->with(static::callback(function DRequest $actualRequest) use ($ids) {
                static::assertEquals($ids$actualRequest->request->all('ids'));

                return true;
            })$cart$context);

        $this->cartService->removeItems($cart$ids$context);
    }

    public function testRemoveCallsRemoveRoute(): void
    {
        $context = $this->createMock(SalesChannelContext::class);
        $cart = new Cart(Uuid::randomHex());

        $id = Uuid::randomHex();

        $this->cartItemRemoveRoute->expects(static::once())
            ->method('remove')
            


                $ids = [];
                foreach ($idData as $key => $id) {
                    if (!\is_string($id)) {
                        throw RoutingException::invalidRequestParameter("ids[{$key}]");
                    }

                    $ids[] = $id;
                }

                $cart = $this->cartService->removeItems($cart$ids$context);

                if (!$this->traceErrors($cart)) {
                    $this->addFlash(self::SUCCESS, $this->trans('checkout.cartUpdateSuccess'));
                }
            } catch (\Exception) {
                $this->addFlash(self::DANGER, $this->trans('error.message-default'));
            }

            return $this->createActionResponse($request);
        });
    }

    
Home | Imprint | This part of the site doesn't use cookies.