InvalidPriceDefinitionException example

/** * @throws InvalidPriceDefinitionException * @throws CartException */
    public function calculate(DiscountLineItem $discount, DiscountPackageCollection $packages, SalesChannelContext $context): DiscountCalculatorResult
    {
        /** @var AbsolutePriceDefinition|null $priceDefinition */
        $priceDefinition = $discount->getPriceDefinition();

        if (!$priceDefinition instanceof AbsolutePriceDefinition) {
            throw new InvalidPriceDefinitionException($discount->getLabel()$discount->getCode());
        }

        $fixedTotalPrice = abs($priceDefinition->getPrice());

        $affectedPrices = $packages->getAffectedPrices();

        $discountDiff = $this->getTotalDiscountDiffSum($fixedTotalPrice$packages$affectedPrices);

        // now calculate the correct price         // from our collected total discount price         $discountPrice = $this->absolutePriceCalculator->calculate(
            -
        if ($fixedPricesDiscountLineItems->count() === 1) {
            return $fixedPricesDiscountLineItems;
        }

        // if there are more than one fixed price lineitems in filtered collection         // we are sorting all by lowest fixed price (lowest price to beginning)         $fixedPricesDiscountLineItems->sort(function DLineItem $discountA, LineItem $discountB) {
            $priceDefA = $discountA->getPriceDefinition();
            $priceDefB = $discountB->getPriceDefinition();

            if (!$priceDefA instanceof AbsolutePriceDefinition) {
                throw new InvalidPriceDefinitionException((string) $discountA->getLabel()$discountA->getReferencedId());
            }
            if (!$priceDefB instanceof AbsolutePriceDefinition) {
                throw new InvalidPriceDefinitionException((string) $discountB->getLabel()$discountB->getReferencedId());
            }

            // NEXT-21735 - This is covered randomly             // @codeCoverageIgnoreStart             if ($priceDefA->getPrice() === $priceDefB->getPrice()) {
                return 0;
            }

            

    public function calculate(
        DiscountLineItem $discount,
        DiscountPackageCollection $packages,
        SalesChannelContext $context
    ): DiscountCalculatorResult {
        /** @var AbsolutePriceDefinition|null $priceDefinition */
        $priceDefinition = $discount->getPriceDefinition();

        if (!$priceDefinition instanceof AbsolutePriceDefinition) {
            throw new InvalidPriceDefinitionException($discount->getLabel()$discount->getCode());
        }

        $fixedUnitPrice = abs($priceDefinition->getPrice());

        $totalDiscountSum = 0.0;

        $composition = [];

        foreach ($packages as $package) {
            foreach ($package->getCartItems() as $lineItem) {
                if ($lineItem->getPrice() === null) {
                    
/** * @throws InvalidPriceDefinitionException */
    public function calculate(
        DiscountLineItem $discount,
        DiscountPackageCollection $packages,
        SalesChannelContext $context
    ): DiscountCalculatorResult {
        $definition = $discount->getPriceDefinition();

        if (!$definition instanceof PercentagePriceDefinition) {
            throw new InvalidPriceDefinitionException($discount->getLabel()$discount->getCode());
        }

        $definedPercentage = -abs($definition->getPercentage());

        // now simply calculate the price object         // with that sum for the corresponding line items.         // we dont need to check on the actual item count in there,         // because our calculation does always go for the original cart items         // without considering any previously applied discounts.         $affectedPrices = $packages->getAffectedPrices();

        


    /** * @throws InvalidPriceDefinitionException */
    public function calculate(DiscountLineItem $discount, DiscountPackageCollection $packages, SalesChannelContext $context): DiscountCalculatorResult
    {
        /** @var AbsolutePriceDefinition $definition */
        $definition = $discount->getPriceDefinition();

        if (!$definition instanceof AbsolutePriceDefinition) {
            throw new InvalidPriceDefinitionException($discount->getLabel()$discount->getCode());
        }

        $affectedPrices = $packages->getAffectedPrices();

        $totalOriginalSum = $affectedPrices->sum()->getTotalPrice();
        $discountValue = -min(abs($definition->getPrice())$totalOriginalSum);

        $price = $this->priceCalculator->calculate(
            $discountValue,
            $affectedPrices,
            $context
        );
Home | Imprint | This part of the site doesn't use cookies.