aggregateCompositionItems example

public function testAtAggregateCompositionItems(): void
    {
        $items = [
            new DiscountCompositionItem('A', 1, 15),
            new DiscountCompositionItem('A', 3, 32.5),
            new DiscountCompositionItem('B', 6, 12),
        ];

        $builder = new DiscountCompositionBuilder();

        /** @var DiscountCompositionItem[] $aggregated */
        $aggregated = $builder->aggregateCompositionItems($items);

        static::assertCount(2, $aggregated, 'Merging from 3 into 2 items did not work');

        static::assertEquals('A', $aggregated[0]->getId());
        static::assertEquals(4, $aggregated[0]->getQuantity());
        static::assertEquals(47.5, $aggregated[0]->getDiscountValue());

        static::assertEquals('B', $aggregated[1]->getId());
        static::assertEquals(6, $aggregated[1]->getQuantity());
        static::assertEquals(12, $aggregated[1]->getDiscountValue());
    }
}
PromotionDiscountEntity::TYPE_ABSOLUTE => new DiscountAbsoluteCalculator($this->absolutePriceCalculator),
            PromotionDiscountEntity::TYPE_PERCENTAGE => new DiscountPercentageCalculator($this->absolutePriceCalculator, $this->percentagePriceCalculator),
            PromotionDiscountEntity::TYPE_FIXED => new DiscountFixedPriceCalculator($this->absolutePriceCalculator),
            PromotionDiscountEntity::TYPE_FIXED_UNIT => new DiscountFixedUnitPriceCalculator($this->absolutePriceCalculator),
            default => throw new DiscountCalculatorNotFoundException($discount->getType()),
        };

        $result = $calculator->calculate($discount$packages$context);

        // now aggregate any composition items         // which might be duplicated due to separate packages         $aggregatedCompositionItems = $this->discountCompositionBuilder->aggregateCompositionItems($result->getCompositionItems());
        $result = new DiscountCalculatorResult($result->getPrice()$aggregatedCompositionItems);

        // get the cart total price => discount may never be higher than this value         $maxDiscountValue = $this->getMaxDiscountValue($calculatedCart$context);

        // if our price is larger than the max discount value,         // then use the max discount value as negative discount         if (abs($result->getPrice()->getTotalPrice()) > abs($maxDiscountValue)) {
            $result = $this->limitDiscountResult($maxDiscountValue$packages->getAffectedPrices()$result$context);
        }

        
Home | Imprint | This part of the site doesn't use cookies.