More at spreecommerce.com: Features | Support | Blog | Demo | Community | Download

Coupons and Discounts

This guide explains how to create coupons and discounts using Spree. After reading it, you should be familiar with:

1 Overview

Coupons are also referred to as discount codes or offer codes. Using a coupon consists of typing in a special code associated with the coupon and then receiving a credit adjustment to the order total. Coupons are highly customizable and if the built in functionality is not sufficient, you can easily implement the desired business logic with a custom calculator.

2 Adjustments

Adjustments come in two basic flavors: Charges and Credits. From an implementation perspective, they are both modeled in a single database table called adjustments and use the single table inheritance mechanism of Rails. Charges add to the order total, and credits work in the opposite direction.

Orders have one or more adjustments associated with them and each adjustment also belongs to an adjustment source. This allows charges and credits to recalculate themselves when requested. Adjustments are always recalculated before they are saved which includes every time and order is updated. This provides a very flexible system by which an adjustment can determine that it is no longer relevant based on changes in the order.

Consider a coupon that takes $5 off all orders over $20. If the order exceeds the required amount during checkout the coupon will create the proper adjustment. If the customer then decides to edit their cart (before completing checkout) then you will want to make sure that the coupon still qualifies. If the order total drops below the required amount the source of the adjustment (in this case the coupon) will have the ability to remove the adjustment based on its own internal logic.

3 Eligibility

Coupon eligibility is completely customizable on a per coupon basis. Eligibility is determined by the following factors.

  • Start Date – coupons can be configured to be invalid before a specific date
  • Expiration Date – coupons can be configured so that they are not usable passed a certain date
  • Expiration Date – coupons can be configured so that they are not usable passed a certain date
  • Number of Uses – coupons can be restricted to an arbitrary number of uses (typically a single use if there’s a limit at all)
  • Combination – there is an option to restrict specific coupons so that they cannot be combined with other coupons in the same order.

Any other restriction on eligibility is intended to be provided by custom calculators. The compute method has access to the complete order (including shipping and other related information) and can simply return nil if the coupon is not to be applied in a specific situation

4 Discount Calculation

The create_discount method in Coupon is responsible for the actual calculation of the credit to be applied for the cooupon. By default, Spree will not allow the credit amount to exceed the item total. The credit adjustment associated with a coupon is subject to recalculation based on changes in the order. This is no different then any other adjustment to the order total (such as shipping or tax charges.)

Please be sure to read the section on calculators for a more complete understanding of this crucial concept.

5 Gift Certificates

Gift certificates are nearly identical to a coupon. The major difference being that gift certificates are purchased through the site for use in the future. Spree does not currently provide support for gift certificates but adding such support would be trivial. We will most likely provide this functionality in the near future as an extension.