createTestFixturePercentagePromotion example

public function test100PercentageDiscount(): void
    {
        $productId = Uuid::randomHex();
        $promotionId = Uuid::randomHex();
        $code = 'BF' . Random::getAlphanumericString(5);
        $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');
        
$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);

        $cart = $this->getCart($browser, TestDefaults::SALES_CHANNEL);
        static::assertCount(2, $cart['lineItems']);
    }

    

    public function testAddLineItemAddsToExtension(): void
    {
        $productId = Uuid::randomHex();
        $promotionId = 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);

        

    public function testPromotionNotAddedWithoutProduct(): 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);

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

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

    /** * This test verifies that our promotions are correctly * removed when also removing the last product * * @group promotions */

        $productId1 = Uuid::randomHex();
        $productId2 = Uuid::randomHex();
        $promotionId = Uuid::randomHex();
        $code = 'BF19';

        // 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->createTestFixturePercentagePromotion($promotionId$code, 25, null, $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
$productGross = 123;
        $percentage = 50;
        $maxValueGlobal = 40.0;

        $expectedPrice = $deliveryCosts - $maxValueGlobal;
        $expectedTotal = $expectedPrice + $productGross;

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

        $this->createTestFixturePercentagePromotion($promotionId$code$percentage$maxValueGlobal$this->getContainer(), PromotionDiscountEntity::SCOPE_DELIVERY);

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

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

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

        static::assertEquals($expectedPrice$cart->getDeliveries()->getShippingCosts()->sum()->getTotalPrice());
        static::assertEquals($expectedTotal$cart->getPrice()->getTotalPrice());
        
// now remove our cart, make sure our remove event is only called once         $this->cartService->remove($cart$productId$this->getContext());
    }

    /** * @param list<float> $codes */
    private function createBulkPromotions(array $codes): void
    {
        foreach ($codes as $percentage) {
            $this->createTestFixturePercentagePromotion(
                Uuid::randomHex(),
                (string) $percentage,
                $percentage,
                null,
                $this->getContainer()
            );
        }
    }
}

        $productId = Uuid::randomHex();
        $promotionId1 = Uuid::randomHex();
        $promotionId2 = Uuid::randomHex();
        $context = $this->getContainer()->get(SalesChannelContextFactory::class)->create(Uuid::randomHex(), TestDefaults::SALES_CHANNEL);

        // 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());
        
Home | Imprint | This part of the site doesn't use cookies.