PriceCalculationService.GetProductAttributeValuePriceAdjustmentAsync should be able to not include discounts

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
2 years ago
The method ShoppingCartService.GetUnitPriceAsync has the "bool includeDiscounts" argument.

At some point it calls the method PriceCalculationService.GetProductAttributeValuePriceAdjustmentAsync which calls GetFinalPriceAsync without specifying a value for the argument includeDiscounts, which defaults to true.

So even if I call ShoppingCartService.GetUnitPriceAsync with includeDiscounts: false, that PriceCalculationService.GetFinalPriceAsync call is made with includeDiscounts: true.

In addition to produce a wrong result (if I set includeDiscounts = false, I assume that discounts will not be applied to product attributes too), this makes impossible to develop discount rules based on cart items amount (at least without rewriting several methods of ShoppingCartService and PriceCalculationService), since it could lead to infinite recursion when checking the requirements.

The method PriceCalculationService.GetProductAttributeValuePriceAdjustmentAsync should have "includeDiscounts" argument (which defaults to true), so it  can pass it to GetFinalPriceAsync.
2 years ago
Method GetFinalPrice returns both finalPrice and priceWithoutDiscounts, and as you can see priceWithoutDiscounts is used in GetProductAttributeValuePriceAdjustment method.
So, by default discounts will not be applied to product attributes. If you need a different behavior, you can customize it.
2 years ago
Thanks Romanov for the response.

You're right. The used price is always priceWithoutDiscounts.
In 4.40.3 "Item1" notation is used so I didn't noticed at first.

Actually, my issue was the second part:
matteo.melli wrote:
... this makes impossible to develop discount rules based on cart items amount (at least without rewriting several methods of ShoppingCartService and PriceCalculationService), since it could lead to infinite recursion when checking the requirements.


I edited the code passing the includeDiscounts value.
But now that I realized, we can just pass includeDiscounts: false.

productPrice = (await GetFinalPriceAsync(product, customer, includeDiscounts: false)).priceWithoutDiscounts;
[...]
adjustment = (await GetFinalPriceAsync(associatedProduct, customer, includeDiscounts: false)).priceWithoutDiscounts * value.Quantity;


This avoids the execution of this block
                if (includeDiscounts)
                {
                    //discount
                    var (tmpDiscountAmount, tmpAppliedDiscounts) = await GetDiscountAmountAsync(product, customer, price);
                    price -= tmpDiscountAmount;

                    if (tmpAppliedDiscounts?.Any() ?? false)
                    {
                        discounts.AddRange(tmpAppliedDiscounts);
                        appliedDiscountAmount = tmpDiscountAmount;
                    }
                }

which is useless and might lead to infinite recursion.

Don't you agree?
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.