Discounts
Discounts can be applied to the cart, checkout, order items, or the entire order. This means discounts can flexibly apply to specific order items or the entire cart/order.
In this tutorial, you will learn how to display discounts in your theme.
Requirements
You have already created a cart template or a customer/order template.
Resources
To display discounts in your theme, you will use the following:
discount_application
object
When a discount is applied in the cart, order, or checkout, a discount_allocation object will exist. Depending on where the discount is applied, you can access the corresponding discount information through the parent object.
- For the cart template:
discount_allocation
object
The discount_allocation object links a discount_application
to an order item.
You can access all discount information related to an order item using the line_item.line_level_discount_allocations property.
line_item
object
To complete the price display, you will use the price and discount properties of the line_item object, including:
Implementing Discount Displays
Since discounts can be applied to order items, the entire cart, or the order, you need to display discount information in two places:
This tutorial uses the cart as an example. Similarly, you can adjust these examples to use the order object to add discounts to the customer/order template.
You can implement these components directly in the Handlebars template or within a section of the JSON template.
Line Item Discounts
If an order item has a discount, the discount needs to be displayed within the order item. To display a discount for an order item, you need to show the following:
Line Item Price
If a discount is applied to an order item, the original price should be struck through, and the new discounted price should be displayed. You can use the Handlebars line_item object to access the following properties:
Line Item Discounts
If a discount is applied to an order item, each discount and its associated amount or the total discount amount should be displayed. You can access the specific discounts for an order item using the line_item object's line_level_discount_allocations property and the total line item discount via the line_level_total_discount property.
Example
Below is an example of displaying the price and discount information:
<s></s>
<span></span>
<span>Discounts:</span>
<ul>
<li>
-
</li>
</ul>
For another example of displaying line item discounts, see Seed's implementation.
Cart Discounts
The subtotal is the sum of the order items after applying the line item discounts, and the total is the cart amount after applying the cart-level discounts. If discounts apply to the whole cart, they should be shown between the subtotal and total.
Cart-level discounts can be accessed via the cart or order object's cart_level_discount_applications
property.
<strong>Subtotal:</strong>
<strong>Discounts:</strong>
<ul>
<li>
-
</li>
</ul>
<strong>Total:</strong>
For another example of displaying order item discounts, see Seed's implementation.
Error loading component.