getDiscountValue example

/** * {@inheritdoc} */
    public function addProportionalDiscount(DiscountContext $discountContext)
    {
        $prices = $this->getPositionPrices($discountContext);
        $hasMultipleTaxes = $this->calculator->hasDifferentTaxes($prices);

        if ($discountContext->getDiscountType() === self::DISCOUNT_ABSOLUTE) {
            $discounts = $this->calculator->calculate(
                $discountContext->getDiscountValue(),
                $prices,
                $discountContext->isNetPrice()
            );
        } else {
            $discounts = $this->calculator->recalculatePercentageDiscount(
                $discountContext->getDiscountValue(),
                $prices,
                $discountContext->isNetPrice()
            );
        }

        
/** * @param DiscountCompositionItem[] $items */
    public function buildCompositionPayload(array $items): array
    {
        $payloadItems = [];

        foreach ($items as $item) {
            $payloadItems[] = [
                'id' => $item->getId(),
                'quantity' => $item->getQuantity(),
                'discount' => $item->getDiscountValue(),
            ];
        }

        return $payloadItems;
    }

    /** * If our discount price is greater than our actual cart price, we have to * adjust the calculated discount price. * Due to that, we also have to adjust our composition data to match the new target price. */
    


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