Updating your Liquid templates for Scripts
If you run scripts in your online store, then you need to check how they affect the pages in your storefront. Many themes include code to support your scripts, but some do not. If your theme doesn't have the code, then you can add it.
The most recent release of the Timber framework, and the following official Shopify Themes, support scripts:
Most issues with scripts and themes involve line item scripts that offer discounts. For example, the total price of the order is correct, but the line item prices do not show the discounts. Customers need to understand how their discounts are calculated. They want to see the original and discounted price as well as a short description of the discount. If any of these details are missing from your cart, then you need to add the Liquid code so that they appear.
A good way to check how your script affects your storefront is to visit as a customer and perform the actions that trigger the script.
Liquid object attributes
The following lists contain Liquid object attributes that are commonly needed to support scripts in your store's cart:
Cart object attributes:
Line item attributes:
- The scripts object can be used to return information about a store's active scripts. This object can be useful when debugging scripts.
The following example describes the changes that you can make to your cart.liquid template to support a line item script.
For example, if you have a cart with three line items:
- Soccer ball
- Quantity: 1
- Unit price: $15.00
- Tennis ball
- Quantity: 5
- Unit price: $5.00
- Running shoe
- Quantity: 1
- Unit price: $30.00
And you have published a script that applies the following discounts:
- 10% off two or more tennis balls
- $5 off all shoes
cart.liquid template might be a simple table listing the line items and a summary of their total amounts:
With the discounts applied by the script, this template creates the following table for your cart:
Update the line items
To show the discounts applied by the script, we need to update the line items to show three key pieces of information:
- the line price before discounts
- the line price after discounts
- a message describing the discounts that were applied.
To do this, use the following Liquid object attributes:
line_item.total_discountreturns the amount of discount that has been applied to the line item
line_item.original_line_pricereturns the line item price before discounts
line_item.messagereturns a message that describes the discounts that were applied to the line item.
It can help to differentiate the original line price from the discounted line price using a strikethrough effect:
Your cart should now look like this:
Update the cart summary
To help the customer keep track of their order price, you should also show:
- the original subtotal of the cart, to let customers compare the before and after discount totals
- the total savings the customer received on their cart.
Again, to add this information, use the following Liquid object attributes:
cart.total_discountreturns the amount of discount applied to items in the cart
cart.original_total_pricereturns the subtotal of the cart before discounts.
With this information, your updated
.summary block might look like this:
Your cart table should now look like this:
By adding a few new Liquid objects to your templates, you can help your customers understand how their discounts are calculated.
The following Liquid example shows the discounts for each line item:
See another discount example that includes the Liquid code changes.