Issue with product specific Discounts that use a coupon code and product price caching enabled

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
2 years ago
Repro using nopCommerce latest, but this one has been around for a long time:

- Enable Product Price Caching in catalog settings > performance panel
- Create a product specific discount (Assigned To Products) with a any non-empty coupon code
- In public store, add discounted product to cart
- On cart page, add and remove discount.  
- Note that the discount message appears incorrectly.  

I'm assuming now any customer will or won't get this discount, depending on what's cached, with or without the coupon code.

I peeked at source code, (PriceCalculationService > GetFinalPrice) and I'm imagining we just pull the discount related logic outside of the cache get.

PS: should I repost this on Github?
2 years ago
Please see the comments here.
2 years ago
There's no intentions of fixing this?  Why not disallow and hide product coupons altogether in this scenario?  It seems messy to be just a bug and a hint.

I'm tasked with finding a solution other than disabling price cache, so I'll keep you guys posted in this thread.

-Adam
2 years ago
You are very welcome to suggest your solution to this issue.
2 years ago
The fix was pretty simple - Implement separate caching for retrieving discount amounts, and move the discount-related block of code outside of the cache call.  

Another architectural idea for future consideration:  in GetFinalPrice, accept a list of CustomerRoles and CouponCodes as args in the core function, and the current core function accpting a customer arg becomes an overload.  

We continue to appreciate everything you're doing with nopCommerce, thanks guys!
2 years ago
RomanovM wrote:
You are very welcome to suggest your solution to this issue.


How about including coupon codes in the cache key, which would take care of the main issue, as far as I've encountered so far:

todo: cache clearing prices when discounts are updated

PriceCalculationService.cs starting line 335


   if (product == null)
                throw new ArgumentNullException(nameof(product));

            var couponCodes = new List<string>();

            if (includeDiscounts)
            {
                 couponCodes = (await _customerService.ParseAppliedDiscountCouponCodesAsync(customer)).ToList().OrderBy(q=>q).ToList();
            }

            var cacheKey = _staticCacheManager.PrepareKeyForDefaultCache(NopCatalogDefaults.ProductPriceCacheKey,
                product,
                overriddenProductPrice,
                additionalCharge,
                includeDiscounts,
                quantity,
                await _customerService.GetCustomerRoleIdsAsync(customer),
                await _storeContext.GetCurrentStoreAsync(),
                couponCodes);

2 years ago
Adam, thanks a lot for the suggestion. But first of all, it'll significantly increase the cache size (multiply by a number of all existing coupon codes).

Furthermore, it won't work when some discount requirement rules are applied. For example, minimum order subtotal (or when some other product should be in the shopping cart)
2 years ago
Thanks Andrei,

In my experience, I've rarely if ever seen a nop Discount that doesn't require a coupon code, and I'm assuming that's the main benefit of being within the cache.  I feel strongly that, although a user shouldn't ignore the help tooltip, this one can do some harm.

Would there be any other reason not to pull Discount items out of the cache?  Isn't there more benefit in always caching (no setting)?

We continue to appreciate nopCommerce and all of your efforts,
-Adam
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.