Creating a simple coupon code discount

Coupon code discounts are one of the simplest discount types. All new EPiServer promotions can be configured to be applied with coupon code. In this article, I will show how to add a coupon code to an order and create a simple order discount using it.

Adding coupon codes to an order is simple. Actually, add the coupon code to the cart (which is a kind of order).

public void SetCuponCode(string cuponCode)
{
    if (string.IsNullOrEmpty(cuponCode))
    {
        return;
    }

    var orderForm = (IOrderForm) CartHelper.Cart.OrderForms.First();
    if (orderForm.CouponCodes.Contains(cuponCode))
    {
        return;
    }
    orderForm.CouponCodes.Add(cuponCode);
}

Coupon codes can be set on the cart's order form but as CouponCodes property is defined as an explicit IOrderForm interface implementation, the order form has to be cast to IOrderForm. Then add a coupon code to the CouponCodes property if it is not already added.

Do not forget to run cart validation workflow and save changes.

OrderGroupWorkflowManager.RunWorkflow(CartHelper.Cart, OrderGroupWorkflowManager.CartValidateWorkflowName);
CartHelper.Cart.AcceptChanges();

Next step would be creating a coupon code field in the cart UI, post it to the cart controller, and use this method to set coupon codes.

Then editors will be able to create new discounts with coupon codes. The simplest discount type for that is Discount off Total Order Value.

Discount off Total Order Value option

Set coupon code in the Promotion code field.

Coupon code field - promotion code

And set what amount do get off of the total order value - percentage or the exact amount. Notice that Spend at least has to be set too and it should be more than zero.

Off total order value fields

Now simple coupon code discount is created and can be used on the site.