addPromotionCode example

 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->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 */
// 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());
    }

    /** * This test verifies that our percentage promotions are correctly added. * We add a product and also an percentage promotion. * Our final delivery price should then be as expected. * * @group promotions * * @throws CartException */
        $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 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 */
        $composition = $discountItem->getPayload()['composition'];

        static::assertEquals($productId1$composition[0]['id']);
        static::assertEquals(3, $composition[0]['quantity']);
        
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 */
    public function testPromotionsRemovedWithProduct(): void
    {
        $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 */
// 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 */
// 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 */
$dispatcher = $this->getContainer()->get('event_dispatcher');

        $addListener = $this->getMockBuilder(CallableClass::class)->getMock();
        $addListener->expects(static::exactly(1 + \count($codes)))->method('__invoke');
        $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 */
Home | Imprint | This part of the site doesn't use cookies.