Adding coupon code inputs to pages

The Xperience E-commerce Solution can process coupon codes for several types of discounts and offers:

Customers who obtained a coupon code are eligible to receive a discount or redeem a gift card (based on the configuration of the discount or gift card). When a customer inputs a coupon code, the system validates whether it provides a discount.

The CMS.Ecommerce assembly (installed into your project as part of the Kentico.Xperience.Libraries integration package) provides an IShoppingService interface alongside its default implementation, the ShoppingService, that exposes API for working with coupon codes.

Adding coupon code support

The following scenario demonstrates how to add a coupon code input field on a page:

  1. Open your MVC project in Visual Studio.

  2. Edit the controller class used to manage your checkout process (see Integrating the shopping cart).

  3. Implement two POST actions – one for adding coupon codes to the current shopping card and another for removing them.

    • To add and validate coupon codes, call the IShoppingService.AddCouponCode method. The method applies the specified coupon code to the current customer’s shopping cart and performs a recalculation of the cart’s contents.

                /// <summary>
                /// Adds the specified coupon code to the shopping cart.
                /// </summary>
                public ActionResult AddCouponCode(string couponCode)
                    // Adds the coupon code to the shopping cart
                    if ((couponCode == "") || !shoppingService.AddCouponCode(couponCode))
                        // Adds an error message to the model state if the entered coupon code is not valid
                        ModelState.AddModelError("CouponCodeError", "The entered coupon code is not valid.");
                    // Initializes the shopping cart model
                    ShoppingCartViewModel model = new ShoppingCartViewModel(shoppingService.GetCurrentShoppingCart());
                    // Displays the shopping cart
                    return View("ShoppingCart", model);

      The AddCouponCode method returns a true value if the specified coupon code provides a valid discount for the current shopping cart and customer.

    • To remove coupon codes, call the IShoppingService.RemoveCouponCode method.

                /// <summary>
                /// Removes the specified coupon code from the shopping cart.
                /// </summary>
                public ActionResult RemoveCouponCode(string couponCode)
                    // Removes the specified coupon code
                    // Displays the shopping cart
                    return RedirectToAction("ShoppingCart");
  4. Perform the following changes in the view:

    1. Create a form with a coupon code text input and an apply button (send POST requests to the action that adds coupon codes).
    2. Display all coupon codes applied to the current shopping cart. To add the code strings to the data of your view model, select the values from the CouponCodes.AllAppliedCodes collection of the shopping cart object (ShoppingCartInfo).
    3. For each applied coupon code, add a form with a single button that allows customers to remove the given coupon code (send POST requests to the action that removes coupon codes).

@* Renders a text box for entering coupon codes and a button invoking
    an action that handles adding of new coupon codes. *@
@using (Html.BeginForm("AddCouponCode", "Checkout", FormMethod.Post))
    <input type="text" name="CouponCode" />
    <input type="submit" value="Apply" />
@* Lists applied coupon codes with an option to remove them. *@
    @* Loops through all applied coupon codes *@
    @foreach (string couponCode in Model.CouponCodes)
            @* Renders a button invoking the action that removes specified coupon codes. *@
            @using (Html.BeginForm("RemoveCouponCode", nameof(CheckoutController), FormMethod.Post))
                <input type="hidden" name="CouponCode" value="@couponCode" />
                <input type="submit" value="Remove" />

Customers are now able to redeem coupon codes during the first step of the checkout process.