---
title: Adding coupon code fields to MVC checkout processes
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

The Kentico E-commerce Solution can process coupon codes for several types of [discounts and offers](https://docs.kentico.com/k11/e-commerce-features/managing-your-store/discounts.md):

- [Product coupons](https://docs.kentico.com/k11/e-commerce-features/managing-your-store/discounts/working-with-product-coupons.md)
- [Gift cards](https://docs.kentico.com/k11/e-commerce-features/managing-your-store/discounts/working-with-gift-cards.md)
- [Order discounts](https://docs.kentico.com/k11/e-commerce-features/managing-your-store/discounts/working-with-order-discounts.md)
- [Buy X Get Y discounts](https://docs.kentico.com/k11/e-commerce-features/managing-your-store/discounts/working-with-buy-x-get-y-discounts.md)
- [Free shipping offers](https://docs.kentico.com/k11/e-commerce-features/managing-your-store/discounts/working-with-free-shipping-offers.md)

[Customers](https://docs.kentico.com/k11/e-commerce-features/managing-your-store/customers.md) 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 building a [checkout process on an MVC site](https://docs.kentico.com/k11/developing-websites/developing-sites-using-asp-net-mvc/developing-on-line-stores-in-mvc/checkout-process-in-mvc.md), you may want to add an input text box that allows customers to enter coupon codes. The system validates whether a specified coupon code provides a discount. In Kentico MVC projects, the [Kentico.Ecommerceintegration package](https://docs.kentico.com/k11/developing-websites/developing-sites-using-asp-net-mvc/starting-with-mvc-development/installing-kentico-integration-packages.md) provides an API for working with coupon codes.

To add a coupon code input to your MVC site:

1. Open your MVC project in Visual Studio.
2. Edit the controller class that you use for your checkout process (see [Using a shopping cart on MVC sites](https://docs.kentico.com/k11/developing-websites/developing-sites-using-asp-net-mvc/developing-on-line-stores-in-mvc/checkout-process-in-mvc/using-a-shopping-cart-on-mvc-sites.md)).
3. Implement two POST actions – one for adding coupon codes to the current shopping card and another for removing coupon codes.

   - To add and validate coupon codes, call the **AddCouponCode** method of the _ShoppingCart_ object representing the current shopping cart.

     > **Info:** 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 **RemoveCouponCode** method of the _ShoppingCart_ object.

     ```csharp

             /// <summary>
             /// Adds the specified coupon code to the shopping cart.
             /// </summary>        
             [HttpPost]
             public ActionResult AddCouponCode(string couponCode)
             {
                 // Gets the current user's shopping cart
                 ShoppingCart currentCart = shoppingService.GetCurrentShoppingCart();

                 // Adds the coupon code to the shopping cart
                 // The 'ShoppingCart.AddCouponCode' method automatically recalculates the shopping cart, 
                 // so there is no need to call the Evaluate() method after
                 if ((couponCode == "") || !currentCart.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
                 {
                     // Assigns the current shopping cart to the model
                     Cart = currentCart,
                     RemainingAmountForFreeShipping = pricingService.CalculateRemainingAmountForFreeShipping(currentCart)
                 };

                 // Displays the shopping cart
                 return View("ShoppingCart", model);
             }

             /// <summary>
             /// Removes the specified coupon code from the shopping cart.
             /// </summary>
             [HttpPost]
             public ActionResult RemoveCouponCode(string couponCode)
             {
                 // Gets the current user's shopping cart
                 ShoppingCart currentCart = shoppingService.GetCurrentShoppingCart();

                 // Removes the specified coupon code
                 // The 'ShoppingCart.RemoveCouponCode' method automatically recalculates the shopping cart, 
                 // so there is no need to call the Evaluate() method after
                 currentCart.RemoveCouponCode(couponCode);

                 // Displays the shopping cart
                 return RedirectToAction("ShoppingCart");
             }


     ```
4. Perform the following changes in your shopping cart 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 applied coupon codes from the **AppliedCouponCodes** collection of the current shopping cart object.
   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).

```csharp

@* Coupon code text box and apply button *@
@using (Html.BeginForm("AddCouponCode", "Checkout", FormMethod.Post))
{
    <input type="text" name="CouponCode" />
    <input type="submit" value="Apply" />
    @Html.ValidationMessage("CouponCodeError")
}
@* List of applied coupon codes with remove buttons *@
<ul>
    @* Loops through all applied coupon codes *@
    @foreach (string couponCode in Model.Cart.AppliedCouponCodes)
    {
        <li>
            @couponCode

            @using (Html.BeginForm("RemoveCouponCode", "Checkout", FormMethod.Post))
            {
                <input type="hidden" name="CouponCode" value="@couponCode" />
                <input type="submit" value="Remove" />
            }
        </li>
    }
</ul>


```

The system now accepts coupon codes typed during the checkout process and customers can get their discounts.
