IDiscountRequirementRule.CheckRequirementAsync Is it possible to get current being checked Product for this method?

6 месяцев назад
Hello,
As I can see IDiscountRequirementRule.CheckRequirementAsync is executed for every Product separately. During CheckRequirementAsync execution I need to make a check in this method only against the currently being checked Product. Is it possible? DiscountRequirementValidationRequest only has Customer and Store models. I need to get a Product model somehow. Any advice?!
6 месяцев назад
someword wrote:
... IDiscountRequirementRule.CheckRequirementAsync is executed for every Product separately...

I don't think so.  Dealing with "products separately" may be the case to calculate a specific discount amount, but "requirement rules" are evaluated per cart.
See this example that shows getting entire cart to see the items:
https://www.nopcommerce.com/en/has-one-product-discount-requirement-rule
6 месяцев назад
Yes, it is. Please see IPriceCalculationService.GetAllowedDiscountsAsync method.

The current solution for this is to override IPriceCalculationService.GetAllowedDiscountsAsync method:

protected override async Task<IList<Discount>> GetAllowedDiscountsAsync(Product product, Customer customer)
{
    customer.AdminComment = JsonSerializer.Serialize(product);

    return await base.GetAllowedDiscountsAsync(product, customer);
}

And consume Customer.AdminComment property in IDiscountRequirementRule.CheckRequirementAsync method by using the Customer model provided by that method.

Of course, another Customer or Store unneeded property can be used to store the needed Product model.