How to check if a coupon was successfully applied

When a customer add a coupon to his/her cart, it’s nice and best practice to show to him/her if the coupon has been applied successfully, or if it was an invalid/not applicable code.

Coupon has been applied successfully
Coupon has been applied successfully

How can you do that?

In old promotion system

When old promotion system run, each successfully applied promotion will be presented by an instance of Discount, which has a property named DiscountCode – this is the coupon used for the promotion (it can of course be null if the promotion requires no coupon).

The only problem is that there are several places to look for that:

  • OrderForm.Discounts, which contains order-level discounts
  • Shipment.Discounts, which contains shipping discounts
  • LineItem.Discounts, which contains item-level discounts

So here how we look at it:

        private bool IsCouponApplied(OrderForm orderForm, string code)
        {
            if (orderForm.Discounts.Cast<Discount>().Any(d => d.DiscountCode == code))
            {
                return true;
            }
            if (orderForm.Shipments.SelectMany(c => c.Discounts.Cast<Discount>()).Any(d => d.DiscountCode == code))
            {
                return true;
            }
            if (orderForm.LineItems.SelectMany(c => c.Discounts.Cast<Discount>()).Any(d => d.DiscountCode == code))
            {
                return true;
            }
            return false;
        }

In new promotion system

Things get a little more complicated with the new promotion. It does not care about those Discounts at all.

Our only information is the RewardDescription returned by IPromotionEngine when it tries to apply promotions.

        public bool AddCouponCode(ICart cart, string couponCode)
        {
            var couponCodes = cart.GetFirstForm().CouponCodes;
            if (couponCodes.Contains(couponCode))
            {
                return false;
            }
            couponCodes.Add(couponCode);
            var rewardDescriptions = cart.ApplyDiscounts(_promotionEngine, new PromotionEngineSettings());
            var appliedCoupons = rewardDescriptions.Where(r=>r.Status == FulfillmentStatus.Fulfilled).Select(c => c.Promotion.Coupon.Code);
            var couponApplied = appliedCoupons.Any(c => c.Equals(couponCode, StringComparison.OrdinalIgnoreCase));
            if (!couponApplied)
            {
                couponCodes.Remove(couponCode);
            }
            return couponApplied;
        }

Here’s a little code which adds the coupon code to the cart:

  • If the coupon is already added, skip it.
  • Try to apply promotion with coupon code(s)
  • For each applied promotion, get the coupon code (if there is any)
  • If the coupon is not applied, remove it from the list.

The drawback of this approach is that it only works (well) with the default implementation – only one coupon per promotion. If you have custom implementation which has multiple coupon codes per promotion, it might not run that well.

In the end, it seems like a CouponCode property should be added to RewardDescription class – to make it easier to know if a coupon code has been successfully applied.

Leave a Reply

Your email address will not be published. Required fields are marked *