addProduct example

// add a new sample product         $this->createTestFixtureProduct($productId, 60, 19, $this->getContainer()$context);

        // add our existing promotions         $this->createTestFixtureAbsolutePromotion($promotionId1, 'sale', 20, $this->getContainer());
        $this->createTestFixturePercentagePromotion($promotionId2, '100off', 100, null, $this->getContainer());

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

        // create product and add to cart         $cart = $this->addProduct($productId, 5, $cart$this->cartService, $context);

        // create promotion and add to cart         $cart = $this->addPromotionCode('sale', $cart$this->cartService, $context);
        $cart = $this->addPromotionCode('100off', $cart$this->cartService, $context);

        static::assertEquals(0.0, $cart->getPrice()->getTotalPrice());
        static::assertEquals(0.0, $cart->getPrice()->getPositionPrice());
        static::assertEquals(0.0, $cart->getPrice()->getNetPrice());
    }

    /** * This test verifies that we can successfully remove an added * promotion by code and get the original price again. * * @group promotions * * @throws CartException */
$code = 'BF';

        // add a new sample product         $this->createTestFixtureProduct($productId, 60, 17, $this->getContainer()$this->context);

        // add a new promotion black friday         $this->createTestFixtureDeliveryPromotion($promotionId, PromotionDiscountEntity::TYPE_ABSOLUTE, 10, $this->getContainer()$this->context, $code);

        $cart = $this->cartService->getCart($this->token, $this->context);

        // create product and add to cart         $cart = $this->addProduct($productId, 2, $cart$this->cartService, $this->context);

        static::assertEquals(100, $cart->getShippingCosts()->getTotalPrice(), 'Delivery costs should be 100 in the beginning');

        // create promotion and add to cart         $cart = $this->addPromotionCode($code$cart$this->cartService, $this->context);

        static::assertEquals(90, $cart->getShippingCosts()->getTotalPrice());

        static::assertEquals(2, $cart->getDeliveries()->count());
    }

    
PromotionFixtureBuilder $promotionBuilder,
        string $productId1,
        string $productId2,
        string $code
    ): Cart {
        $promotionBuilder->buildPromotion();

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

        // add 3 items to our cart         // the cheapest one 1x and 2x the other product         $cart = $this->addProduct($productId1, 2, $cart$this->cartService, $this->context);
        $cart = $this->addProduct($productId2, 1, $cart$this->cartService, $this->context);

        // add our promotion         $cart = $this->addPromotionCode($code$cart$this->cartService, $this->context);

        return $cart;
    }
}
$this->productRepository->create($products$context);
            $downloads = array_merge(...array_column($products, 'downloads'));

            foreach ($downloads as $download) {
                $media = $download['media'];
                $mediaFile = $this->fileFetcher->fetchBlob($media['id']$media['fileExtension'], '');
                $this->fileSaver->persistFileToMedia($mediaFile$media['fileName']$media['id']$context);
            }
        });

        foreach ($ids->prefixed('product-') as $id) {
            $cart = $this->addProduct($id, 1, $cart$this->cartService, $this->salesChannelContext);
        }

        return $cart;
    }

    private function changeTransactionStateToPaid(string $orderId): void
    {
        $transaction = $this->orderTransactionRepository
            ->search(
                (new Criteria())
                    ->addFilter(new EqualsFilter('orderId', $orderId))
                    
$context = $this->getContainer()->get(SalesChannelContextFactory::class)->create(Uuid::randomHex(), TestDefaults::SALES_CHANNEL);

        // add a new sample product         $this->createTestFixtureProduct($productId, 60, 17, $this->getContainer()$context);

        // add a new promotion black friday         $this->createTestFixtureAbsolutePromotion($promotionId$code, 45, $this->getContainer());

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

        // create product and add to cart         $cart = $this->addProduct($productId, 2, $cart$this->cartService, $context);

        // create promotion and add to cart         $cart = $this->addPromotionCode($code$cart$this->cartService, $context);

        static::assertEquals(75.0, $cart->getPrice()->getTotalPrice());
        static::assertEquals(75.0, $cart->getPrice()->getPositionPrice());
        static::assertEquals(64.1, $cart->getPrice()->getNetPrice());
    }

    /** * This test verifies that our promotion components are really involved in our checkout. * We add a product to the cart and apply a code for a promotion with a currency dependent discount. * The standard value of discount would be 15, but our currency price value is 30 * Our cart should have a total value of 70,00 (and not 85 as standard) in the end. * * @group promotions */
// add a new sample product         $this->createTestFixtureProduct($productId1, 50, 19, $this->getContainer()$this->context);
        $this->createTestFixtureProduct($productId2, 100, 19, $this->getContainer()$this->context);

        // add a new promotion         $this->createTestFixtureAbsolutePromotion($promotionId$code, 30, $this->getContainer(), PromotionDiscountEntity::SCOPE_CART);

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

        // create product and add to cart         $cart = $this->addProduct($productId1, 3, $cart$this->cartService, $this->context);
        $cart = $this->addProduct($productId2, 1, $cart$this->cartService, $this->context);

        // create promotion and add to cart         $cart = $this->addPromotionCode($code$cart$this->cartService, $this->context);

        // get discount line item         $discountItem = $cart->getLineItems()->getFlat()[2];

        static::assertTrue($discountItem->hasPayloadValue('composition'), 'composition node is missing');

        /** @var array<int, mixed> $composition */
        
// add a new sample products         $this->createTestFixtureProduct($productId, 100, 19, $this->getContainer()$this->context);
        $this->createTestFixtureProduct($productIdTwo, 100, 7, $this->getContainer()$this->context);

        // add a new promotion         $this->createTestFixtureFixedDiscountPromotion($promotionId, 40, PromotionDiscountEntity::SCOPE_CART, $code$this->getContainer()$this->context);

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

        // add products to cart         $cart = $this->addProduct($productId, 1, $cart$this->cartService, $this->context);
        $cart = $this->addProduct($productIdTwo, 1, $cart$this->cartService, $this->context);

        // add promotion to cart         $cart = $this->addPromotionCode($code$cart$this->cartService, $this->context);

        static::assertEquals(40, $cart->getPrice()->getPositionPrice());
        static::assertEquals(40, $cart->getPrice()->getTotalPrice());
        static::assertEquals(35.49, $cart->getPrice()->getNetPrice());
    }

    /** * if a automatic fixed price promotion (no code necessary) discount is removed * it should not be added again. This is a new feature - to block automatic promotions. * * @group promotions */
public function testPersistOrderAndConvertToCart(): void
    {
        $parentProductId = Uuid::randomHex();
        $childProductId = Uuid::randomHex();
        // to test the sorting, the parentId has to be greater than the rootId         $parentProductId = substr_replace($parentProductId, '0', 0, 1);
        $rootProductId = substr_replace($parentProductId, 'f', 0, 1);

        $cart = $this->generateDemoCart($parentProductId$rootProductId);

        $cart = $this->addProduct($cart$childProductId);

        $product1 = $cart->get($parentProductId);
        $product2 = $cart->get($childProductId);

        static::assertNotNull($product1);
        static::assertNotNull($product2);

        $product1->getChildren()->add($product2);
        $cart->remove($childProductId);

        $cart = $this->getContainer()->get(Processor::class)
            
public function testUpdatingPromotionAfterUpdateProductLineItem(): void
    {
        $salesChannelContext = $this->getContainer()->get(SalesChannelContextFactory::class)->create(Uuid::randomHex(), TestDefaults::SALES_CHANNEL);

        $productId = Uuid::randomHex();
        $promotionCode = 'BF99';

        $this->createTestFixtureProduct($productId, 119, 19, $this->getContainer()$salesChannelContext);

        $browser = $this->createCart(TestDefaults::SALES_CHANNEL);

        $this->addProduct($browser, TestDefaults::SALES_CHANNEL, $productId);

        // Save promotion to database         $this->createTestFixturePercentagePromotion(Uuid::randomHex()$promotionCode, 100, null, $this->getContainer());

        // Add promotion code to our cart (not existing in DB)         $this->addPromotionCodeByAPI($browser, TestDefaults::SALES_CHANNEL, $promotionCode);

        $cart = $this->getCart($browser, TestDefaults::SALES_CHANNEL);

        $this->updateLineItemQuantity($browser, TestDefaults::SALES_CHANNEL, $cart['lineItems'][0]['id'], 3);

        
$this->addEventListener($dispatcher, BeforeLineItemAddedEvent::class$addListener);

        $cart = $this->cartService->getCart($this->getContext()->getToken()$this->getContext());

        // add all our prepared test fixtures         // and promotions to our current cart.         foreach ($codes as $code) {
            $cart = $this->addPromotionCode((string) $code$cart$this->cartService, $this->getContext());
        }

        // now add our product which should trigger our event         $this->addProduct($productId, 1, $cart$this->cartService, $this->getContext());
    }

    /** * This test verifies that we only fire our remove item * once, even though we have lots of promotions in our cart. * * @group promotions */
    public function testAvoidInfiniteLoopEventsWhenRemovingLotsOfPromotions(): void
    {
        $productId = Uuid::randomHex();

        
$promotionCode = 'BF19';

        // add a new sample product         $this->createTestFixtureProduct($productId, 119, 19, $this->getContainer()$this->context);

        // add a new promotion black friday         $this->createTestFixturePercentagePromotion($promotionId$promotionCode, 100, null, $this->getContainer());

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

        // add product to cart         $cart = $this->addProduct($productId, 1, $cart$this->cartService, $this->context);

        // add promotion to cart         $this->addPromotionCode($promotionCode$cart$this->cartService, $this->context);

        /** @var CartExtension $extension */
        $extension = $cart->getExtension(CartExtension::KEY);

        static::assertTrue($extension->hasCode($promotionCode));
    }

    /** * This test verifies that our cart services * does also correctly remove the matching code * within our extension, if existing. * We add a product and promotion code, then we grab the promotion * line item id and remove it. * After that we verify that our code array is empty in our extension. * * @group promotions */
$context = $this->getContainer()->get(SalesChannelContextFactory::class)->create(Uuid::randomHex(), TestDefaults::SALES_CHANNEL);

        // add a new sample product         $this->createTestFixtureProduct($productId, 29, 17, $this->getContainer()$context);

        // add a new promotion black friday         $this->createTestFixturePercentagePromotion($promotionId$code, 100, null, $this->getContainer());

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

        // create product and add to cart         $cart = $this->addProduct($productId, 1, $cart$this->cartService, $context);

        // create promotion and add to cart         $cart = $this->addPromotionCode($code$cart$this->cartService, $context);

        static::assertEquals(0.0, $cart->getPrice()->getPositionPrice(), 'Position Total Price has to be 0,00');
        static::assertEquals(0.0, $cart->getPrice()->getTotalPrice(), 'Total Price has to be 0,00');
        static::assertEquals(0.0, $cart->getPrice()->getCalculatedTaxes()->getAmount(), 'Taxes have to be 0,00');
        static::assertEquals(0.0, $cart->getPrice()->getNetPrice(), 'Net Price has to be 0,00');
    }

    /** * This test verifies that our promotion components are really involved in our checkout. * We add a product to the cart and apply a code for a promotion with 50% discount. * Our cart should have a total value of 15,00 in the end. * * @group promotions * * @throws CartException */

    public function testPromotionsRemovedWithProduct(): void
    {
        $productId = Uuid::randomHex();
        $code = 'BF19';

        $this->createTestFixtureProduct($productId, 119, 19, $this->getContainer()$this->context);
        $this->createTestFixturePercentagePromotion(Uuid::randomHex()$code, 100, null, $this->getContainer());

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

        $cart = $this->addProduct($productId, 1, $cart$this->cartService, $this->context);

        // add our promotion to our cart         $cart = $this->addPromotionCode($code$cart$this->cartService, $this->context);

        $ids = array_keys($cart->getLineItems()->getElements());
        static::assertArrayHasKey(0, $ids);

        // remove our first item (product)         $cart = $this->cartService->remove($cart$ids[0]$this->context);

        static::assertCount(0, $cart->getLineItems());
    }
Home | Imprint | This part of the site doesn't use cookies.