Discount Coupon

Delford Chaffin asked on April 23, 2015 16:28

Consider the following shopping cart:

Image Text

This is an out-of-the-box setup of the demo e-commerce store. I changed the base price of the Configurable PC product from $0 to $10 for this demonstration. I then created a coupon - the "SPRING15A" coupon code is set to take 10% off every product.

When I apply this coupon to the cart, the 10% discount is applied to the base price, but does not account for the product options that affect the price.

This seems very odd to me and is not the way I would have expected it to function. Am I missing something or is this really the way that it works by default and I'm going to have to write my own functionality to make this work?

Thanks!

Recent Answers


Delford Chaffin answered on April 23, 2015 22:07

I ended up writing a custom DiscountCouponInfoProvider and overrode the following methods:

  • ValidateDiscountCouponInternal
  • CalculateUnitDiscountInternal

Here is my code if someone has any thoughts on it or if anyone needs it:

protected override bool ValidateDiscountCouponInternal(ShoppingCartItemInfo item, DiscountCouponInfo discount)
{
    // Do not apply discount coupon to product option (unless percentage) or if it is zero discount or if it is not valid
    if ((item == null) || ((item.IsProductOption && discount.DiscountCouponIsFlatValue)) ||
        (discount == null) || (discount.DiscountCouponValue <= 0) ||
        (item.ShoppingCart == null) || !discount.IsValid)
    {
        return false;
    }
    else
    {
        // Do not apply discount coupon if SKU is not assigned
        bool canBeApplied = SKUDiscountCouponInfoProvider.IsSKUAssignedToDiscountCoupon(item.SKU, discount);
        if (!canBeApplied)
        {
            return false;
        }
    }
    return true;
}



protected override double CalculateUnitDiscountInternal(ShoppingCartItemInfo item, DiscountCouponInfo discount)
{
    // Do not calculate if discount is not valid
    if (!ValidateDiscountCouponInternal(item, discount))
    {
        return 0;
    }

    // Get price after previous discounts
    double newPrice = item.UnitPriceAfterDiscountInMainCurrency;

    double discountValue = discount.DiscountCouponValue;

    // Get correct value for global discount
    if (discount.DiscountCouponIsFlatValue && discount.IsGlobal)
    {
        discountValue = item.ApplyExchangeRateFromGlobalMainCurrency(discount.DiscountCouponValue);
    }

    // Get unit discount
    discountValue = ECommerceHelper.GetDiscountValue(newPrice, discountValue, discount.DiscountCouponIsFlatValue);

    if (discount.DiscountCouponIsFlatValue)
    {
        if (!item.IsProductOption)
        {
            foreach (ShoppingCartItemInfo o in item.ProductOptions)
            {
                newPrice += o.UnitPriceAfterDiscountInMainCurrency;
            }
        }
    }

    // Discount cannot be greater than price
    return (Math.Abs(discountValue) > Math.Abs(newPrice)) ? newPrice : discountValue;
}
1 votesVote for this answer Mark as a Correct answer

Josef Dvorak answered on April 24, 2015 14:25

Hello,

We have been discussing this issue via email, so I am posting the response here as well, should anyone else need to setup the same scenario.

Discount Coupons (called Product coupons in Kentico 8) are indeed applied only to regular SKUs, but not to Product options. We do not consider changing this behaviour in near future so your customization is the best way to achieve this scenario. I went through your code and did not find any issues with it.

On a side note, if you do not need to base your discounts on Coupons, you may want to consider upgrading to Kentico 8, where we introduced a new type of discounts called Catalog discounts. While these do not feature coupon based setting, they do apply to Product options.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.