Try our conversational search powered by Generative AI!

Retrieve the reason when a discount code cannot be applied

Vote:
 

Hi all,

We have some promotions on our website to which i am going to add some redemption limits (1 per customer).

That is all working fine, just one thing that i feel is missing (maybe it is just me not able to find it).

When a discount code is applied to the cart, it eventually fails because it is not applicable, that can be for many reasons (redemption limits, invalid code, no entries in the cart related to that discount code, etc...)

I use : ICart.ApplyDiscounts()

Is there any way to get the reason why the code could not be applied ?

This way i could display a meaningful message to the customer.

#225259
Jul 10, 2020 7:13
Vote:
 

Hi Pierre,

Take a look here in Foundation. In that example, it's creating a PromotionEngineSettings object with an empty constructor. If you look at that class, there is another contructor that you can pass a RequestFulfillmentStatus enum. I am wondering if you were to set that to the value All and then looked at the results that come back, if the RewardDescription for the failed items will have more info about why it failed? There is a property on RewareDescription called Description that is intended for debugging, but, may be helpful in getting the info you need. I hope this helps!

-John

#225266
Jul 10, 2020 20:36
Vote:
 

Hi Pierre,

I think this is not straight forward to show the reason for the customer why the coupon code is not working using the promotion engine. Here are some steps which I used in my current project to display a meaning full message if the coupon code does not work.

  1. Validate the cart, if you get any car validation issue (EPiServer.Commerce.Order.ValidationIssue) then return a particular message. e.g. Cannot process due to missing order status.
  2. Retrieves all coupon codes and check the supplied coupon code does exist . If not then return message e.g. 'Invalid coupon code.'
  3. Create an OrderGroup Extension class and retrieve already applied coupon code on the cart and match to the supplied coupon code if it matches then return message e.g. 'The supplied coupon code already applied'

    /// <summary>
    /// Gets the applied coupon codes for the first order form.
    /// </summary>
    public static ICollection<string> GetCouponCodes(this IOrderGroup orderGroup)
    {
       return orderGroup?.GetFirstForm()?.CouponCodes;
    }
  4. Finally, apply the coupon code on the cart, if it returns false the return a custom message. e.g. 'The coupon code is invalid or expired'
   public bool AddCouponCode(ICart cart, string couponCode)
   {
            var couponCodes = cart.GetCouponCodes();

            if (couponCodes != null && couponCodes.Contains(couponCode))
                return false;

            couponCodes?.Add(couponCode);

            var couponApplied =
                cart.ApplyDiscounts(_promotionEngine, new PromotionEngineSettings())
                    .Where(r => r.Status == FulfillmentStatus.Fulfilled)
                    .Select(c => c.Promotion?.Coupon?.Code)
                    .Where(i => i != null)
                    .Any(c => c.Equals(couponCode, StringComparison.OrdinalIgnoreCase));

            if (!couponApplied)
            {
                couponCodes?.Remove(couponCode);
            }

            return couponApplied;
        }

Thanks,

#225796
Jul 27, 2020 7:49
Vote:
 

Thanks for your answer. Yeah i think i'll have to make my own logic for this.

The main thing i was after was to check if the customer had already applied the coupon in a previous order, for a coupon that has a redemption limit of 1 per customer.

That piece of work has been set aside for now, but will get back to it eventually :)

#226338
Aug 11, 2020 6:33
Vote:
 

The redemption limit will not work for an anonymous user, because how you identify the same user added promo code in the incognito window for the same product or item.

But for the logged-in user, we can traverse all purchase orders (using own custom logic) to check the same coupon code is not applied previously or get the count as redemption limit before applying coupon code on the cart.

#226344
Aug 11, 2020 7:17
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.