calculatePercentage example

case PromotionDiscountEntity::TYPE_PERCENTAGE:
                if (!$originalPriceDefinition instanceof PercentagePriceDefinition) {
                    throw new InvalidPriceDefinitionException((string) $discountLineItem->getLabel()$discountLineItem->getReferencedId());
                }

                $discountMaxValue = '';

                if ($discountLineItem->hasPayloadValue('maxValue')) {
                    $discountMaxValue = $discountLineItem->getPayloadValue('maxValue');
                }

                $discountAdded = $this->calculatePercentage($deliveries$originalPriceDefinition$context$discountMaxValue);

                break;
            case PromotionDiscountEntity::TYPE_FIXED_UNIT:
                if (!$originalPriceDefinition instanceof AbsolutePriceDefinition) {
                    throw new InvalidPriceDefinitionException((string) $discountLineItem->getLabel()$discountLineItem->getReferencedId());
                }

                $discountAdded = $this->calculateFixedDiscount($deliveries$originalPriceDefinition$context$notDiscountedShippingCosts);

                break;
        }

        

    public function discount(float $value): void
    {
        $definition = new QuantityPriceDefinition($this->price->getUnitPrice()$this->price->getTaxRules());
        $definition->setIsCalculated(true);

        $unit = $this->priceStubs->calculateQuantity($definition$this->context);

        $discount = $this->priceStubs->calculatePercentage(\abs($value)new CalculatedPriceCollection([$unit])$this->context);

        $definition = new QuantityPriceDefinition(
            $this->price->getUnitPrice() - $discount->getUnitPrice(),
            $this->price->getTaxRules(),
            $this->getQuantity()
        );

        $this->overwrite($definition);
    }

    /** * `surcharge()` allows a percentage surcharge calculation of the current price scope. The provided value will be ensured to be negative via `abs(value)`. * The provided surcharge is interpreted as a percentage value and will be applied to the unit price and the total price as well. * * @example pricing-cases/product-pricing.twig 34 1 Adds a 10% surcharge to the existing calculated price * * @param float $value The percentage value of the surcharge. The value will be ensured to be negative via `abs(value)`. */
Home | Imprint | This part of the site doesn't use cookies.