DiscountCompositionItem 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');

        
/** @var DiscountCompositionItem $item */
        foreach ($targetItems as $item) {
            $itemTotal = $item->getDiscountValue();

            $factor = 0.0;

            if ($targetPrice->getTotalPrice() > 0) {
                $factor = $itemTotal / $targetPrice->getTotalPrice();
            }

            $compositionItems[] = new DiscountCompositionItem(
                $item->getId(),
                $item->getQuantity(),
                abs($itemTotal) * $factor
            );
        }

        return $compositionItems;
    }

    /** * Iterates through all composition items and removes redundant * occurrences by merging items into single items and * aggregating their values. */
foreach ($packages as $package) {
            foreach ($package->getCartItems() as $lineItem) {
                if ($lineItem->getPrice() === null) {
                    continue;
                }

                $itemTotal = $lineItem->getPrice()->getTotalPrice();

                $factor = $itemTotal / $totalOriginalSum;

                $items[] = new DiscountCompositionItem(
                    $lineItem->getId(),
                    $lineItem->getQuantity(),
                    abs($discountValue) * $factor
                );
            }
        }

        return $items;
    }
}
$items = [];

        foreach ($packages as $package) {
            foreach ($package->getCartItems() as $lineItem) {
                if ($lineItem->getPrice() === null) {
                    continue;
                }

                $itemTotal = $lineItem->getQuantity() * $lineItem->getPrice()->getUnitPrice();
                $percentageFactor = abs($percentage) / 100.0;

                $items[] = new DiscountCompositionItem(
                    $lineItem->getId(),
                    $lineItem->getQuantity(),
                    $itemTotal * $percentageFactor
                );
            }
        }

        return $items;
    }

    private function hasMaxValue(DiscountLineItem $discount): bool
    {
/** * This test verifies that the property is correctly * assigned as well as returned in the getter function. * * @group promotions */
    public function testCompositionItems(): void
    {
        $price = new CalculatedPrice(29, 29, new CalculatedTaxCollection()new TaxRuleCollection());

        $compositionItems = [
            new DiscountCompositionItem('ABC', 2, 13),
        ];

        $result = new DiscountCalculatorResult($price$compositionItems);

        static::assertSame($compositionItems$result->getCompositionItems());
    }
}
foreach ($packages as $package) {
            foreach ($package->getCartItems() as $lineItem) {
                if ($lineItem->getPrice() === null) {
                    continue;
                }

                $itemTotal = $lineItem->getPrice()->getTotalPrice();

                $factor = $totalOriginalSum === 0.0 ? 0 : $itemTotal / $totalOriginalSum;

                $items[] = new DiscountCompositionItem(
                    $lineItem->getId(),
                    $lineItem->getQuantity(),
                    abs($discountValue) * $factor
                );
            }
        }

        return $items;
    }
}
$quantity = $lineItem->getQuantity();
                $itemUnitPrice = $lineItem->getPrice()->getUnitPrice();

                if ($itemUnitPrice > $fixedUnitPrice) {
                    // check if discount exceeds or not, beware of quantity                     $discountDiffPrice = ($itemUnitPrice - $fixedUnitPrice) * $quantity;
                    // add to our total discount sum                     $totalDiscountSum += $discountDiffPrice;

                    // add a reference, so we know what items are discounted                     $composition[] = new DiscountCompositionItem($lineItem->getId()$quantity$discountDiffPrice);
                }
            }
        }

        // now calculate the correct price         // from our collected total discount price         $discountPrice = $this->absolutePriceCalculator->calculate(
            -abs($totalDiscountSum),
            $packages->getAffectedPrices(),
            $context
        );

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