Coupon Codes

Coupon codes are unique strings that may be entered by a user in order to receive a discount.

Discounts are only available in the Pro edition of Craft Commerce.

With Craft Commerce, coupon codes are set up as a condition within a discount promotion.

To create a new discount promotion, navigate to CommercePromotionsDiscounts in the control panel. To see the coupon condition, select to the “Coupon” tab.

An empty coupon field on the discount means no coupon is needed for the discount to work. Adding a coupon requires that a coupon is submitted to the cart. This makes the discount available to match the order but still needs to match all other discount conditions.

Read more about Discounts.

# Using a Coupon Code

To add a coupon to the cart, a customer submits the couponCode parameter using the commerce/cart/update-cart form action:

<form method="post">
  {{ csrfInput() }}
  {{ actionInput('commerce/cart/update-cart') }}
  {{ hiddenInput('successMessage', 'Added coupon code.'|hash) }}
  {{ redirectInput('shop/cart') }}

  <input type="text"
    name="couponCode"
    value="{{ cart.couponCode }}"
    placeholder="{{ "Coupon Code"|t }}"
  >

  <button>Update Cart</button>
</form>

Only one coupon code can exist on the cart at a time, accessible via {{ cart.couponCode }}.

If the customer submits an invalid code, Commerce may update the cart but adds an order notice:







 
 







 






<form method="post">
  {{ csrfInput() }}
  {{ actionInput('commerce/cart/update-cart') }}
  {{ hiddenInput('successMessage', 'Added coupon code.'|hash) }}
  {{ redirectInput('shop/cart') }}

  {# Get the first notice for the `couponCode` attribute, if we have one #}
  {% set couponCodeNotice = cart.getFirstNotice(null, 'couponCode') %}

  {# Get any lower-level coupon code errors just in case #}
  {% set couponCodeError = cart.getFirstError('couponCode') %}

  <input type="text"
    name="couponCode"
    value="{{ cart.couponCode }}"
    class="{% if couponCodeNotice or couponCodeError %}has-error{% endif %}"
    placeholder="{{ "Coupon Code"|t }}"
  >

  <button>Update Cart</button>
</form>

The example above includes cart.getFirstError('couponCode') as a precaution. Commerce won’t throw any coupon errors, but another plugin or custom module could.

You can retrieve the discount associated with the coupon code using craft.commerce.discounts.getDiscountByCode():

{# @var discount craft\commerce\models\Discount #}
{% set discount = craft.commerce.discounts.getDiscountByCode(cart.couponCode) %}
{% if discount %}
  {{ discount.name }} - {{ discount.description }}
{% endif %}