PromotionItemBuilder example


    public function testLineItemType(): void
    {
        $currencyFactor = random_int(0, mt_getrandmax()) / mt_getrandmax();

        $builder = new PromotionItemBuilder();

        $discount = new PromotionDiscountEntity();
        $discount->setId('D5');
        $discount->setType(PromotionDiscountEntity::TYPE_PERCENTAGE);
        $discount->setValue(50);
        $discount->setScope(PromotionDiscountEntity::SCOPE_CART);

        $item = $builder->buildDiscountLineItem('', $this->promotion, $discount, 'C1', $currencyFactor);

        static::assertEquals(PromotionProcessor::LINE_ITEM_TYPE, $item->getType());
    }

    

class PromotionItemBuilderPlaceholderTest extends TestCase
{
    /** * This test verifies that the immutable LineItem Type from * the constructor is correctly used in the LineItem. * * @group promotions */
    public function testLineItemType(): void
    {
        $builder = new PromotionItemBuilder();

        $item = $builder->buildPlaceholderItem('CODE-123');

        static::assertEquals(PromotionProcessor::LINE_ITEM_TYPE, $item->getType());
    }

    /** * This test verifies that we get a correct percentage price of 0 * for our placeholder item. This is important to avoid any wrong * calculations or side effects that could modify the cart amount. * * @group promotions */
$factory = $this->getContainer()->get(ProductLineItemFactory::class);
        $product = $factory->create(['id' => $productId, 'referencedId' => $productId, 'quantity' => $quantity]$context);

        return $cartService->add($cart$product$context);
    }

    /** * Adds the provided code to the current cart. */
    public function addPromotionCode(string $code, Cart $cart, CartService $cartService, SalesChannelContext $context): Cart
    {
        $itemBuilder = new PromotionItemBuilder();

        // ??? currencyPrecision is unused         $lineItem = $itemBuilder->buildPlaceholderItem($code);

        $cart = $cartService->add($cart$lineItem$context);

        return $cart;
    }

    /** * Removes the provided code to the current cart. */
/** * This test verifies that we have the correct payload in our * discount line item. this is used to identify the promotion behind it. * It's also used as reference to individual codes that get marked as redeemed * in the event subscriber, when the order is created. * * @group promotions */
    public function testPayloadStructureBasic(): void
    {
        $builder = new PromotionItemBuilder();

        $discount = new PromotionDiscountEntity();
        $discount->setId('D5');
        $discount->setType(PromotionDiscountEntity::TYPE_ABSOLUTE);
        $discount->setValue(50);
        $discount->setConsiderAdvancedRules(true);
        $discount->setScope(PromotionDiscountEntity::SCOPE_CART);

        $currencyFactor = 0.3;

        $ruleCollection = new RuleCollection();
        
Home | Imprint | This part of the site doesn't use cookies.