addProductToCart example

$this->productRepository->create([
            (new ProductBuilder($ids, 'product'))
                ->price(100)
                ->tax('t1')
                ->visibility()
                ->add('height', 3)
                ->add('width', 3)
                ->add('length', 3)
                ->build(),
        ]$this->salesChannelContext->getContext());

        return $this->addProductToCart($ids->get('product'), 1, $cart$this->cartService, $this->salesChannelContext);
    }

    private function changeTransactionStateToPaid(string $orderId): void
    {
        $transaction = $this->orderTransactionRepository
            ->search(
                (new Criteria())
                    ->addFilter(new EqualsFilter('orderId', $orderId))
                    ->addSorting(new FieldSorting('createdAt', FieldSorting::DESCENDING)),
                $this->salesChannelContext->getContext()
            )->first();
        
->stock(100)
            ->price(100)
            ->visibility();

        // the product builder has a helper function to write the product values to the database, including all dependencies (rules, currencies, properties, etc)         $product->write($this->getContainer());

        $context = $this->getContext();
        $context = $this->login($context);

        // now we test that the product can be added to a customers cart         $cart = $this->addProductToCart($product->id, $context);

        $this->assertLineItemInCart($cart$product->id);

        $this->assertLineItemUnitPrice($cart$product->id, 100);

        $this->assertLineItemTotalPrice($cart$product->id, 100);

        $orderId = $this->mailListener(function DMailEventListener $listener) use ($cart$context) {
            $orderId = $this->order($cart$context);

            $listener->assertSent('order_confirmation_mail');

            

    public function testPriceInCart(ProductBuilder $builder, float $expected, array $contextOptions = []): void
    {
        // the product builder has a helper function to write the product values to the database, including all dependencies (rules, currencies, properties, etc)         $builder->write($this->getContainer());

        $context = $this->getContext(Uuid::randomHex()$contextOptions);

        // `addProductToCart` is a small generic helper method to create a product and add it into the cart within as one liner         $cart = $this->addProductToCart($builder->id, $context);

        static::assertTrue($cart->has($builder->id));

        $item = $cart->get($builder->id);

        static::assertInstanceOf(LineItem::class$item);

        static::assertEquals($builder->id, $item->getId());

        static::assertInstanceOf(CalculatedPrice::class$item->getPrice());
        static::assertEquals($expected$item->getPrice()->getTotalPrice());
    }
Home | Imprint | This part of the site doesn't use cookies.