Creating a simple coupon code discount
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.
Set coupon code in the Promotion code field.
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.
Now simple coupon code discount is created and can be used on the site.