Updating your Liquid templates for Scripts

After you've tested the logic of your scripts in the Script Editor, 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.

Shopify Plus

Scripts and the Script Editor app are available to Shopify Plus merchants only.

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 will trigger the script.

Liquid object attributes

The following lists contain Liquid object attributes that are commonly needed to support scripts in your online store's cart:

Cart object attributes:

Line item attributes:

Scripts object:

  • The scripts object can be used to return information about a store's active scripts. This object can be useful when debugging scripts.

Example

The following example describes the changes that you can make to your cart.liquid template to support a line item script.

Example cart

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

Then your cart.liquid template might be a simple table listing the line items and a summary of their total amounts:

<table class="cart">
  <thead class="heading">
    <tr>
      <th>Product</th>
      <th>Quantity</th>
      <th>Total</th>
    </tr>
  </thead>

  <tbody class="line-items">
    {% for item in cart.items %}
    <tr>
      <td>{{ item.product.title }}</td>
      <td>{{ item.quantity }}</td>
      <td>{{ item.line_price | money }}</td>
    </tr>
    {% endfor %}
  </tbody>

  <tfoot class="summary">
    <tr>
      <td colspan="2">Total</td>
      <td>{{ cart.total_price | money }}</td>
    </tr>
  </tfoot>
</table>

With the discounts applied by the script, this template creates the following table for your cart:

Product Quantity Total
Soccer ball 1 $15.00
Tennis ball 5 $22.50
Running shoe 1 $25.00
Total $62.50

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, you'll use the following Liquid object attributes:

  • line_item.total_discount returns the amount of discount that has been applied to the line item
  • line_item.original_line_price returns the line item price before discounts
  • line_item.message returns 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:

<tbody class="line-items">
  {% for item in cart.items %}
  <tr>
    <td>{{ item.product.title }}</td>
    <td>{{ item.quantity }}</td>
    <td>
      {{ item.line_price }}
      {% if item.total_discount > 0 %}
        <s>{{ item.original_line_price }}</s>
        ( {{ item.message }} )
      {% endif %}
    </td>
  </tr>
  {% endfor %}
</tbody>

Your cart should now look like this:

Product Quantity Total
Soccer ball 1 $15.00
Tennis ball 5 $22.50 $25.00 (10% off two or more tennis balls)
Running shoe 1 $25.00 $30.00 ($5 off all shoes)
Total $62.50

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 allow customers to compare the before and after discount totals
  • the total savings the customer received on their cart.

Again, to add this information you'll use the following Liquid object attributes:

  • cart.total_discount returns the amount of discount applied to items in the cart
  • cart.original_total_price returns the subtotal of the cart before discounts.

With this information, your updated .summary block might look like this:

<tfoot class="summary">
  <tr>
    <td colspan="2">Subtotal</td>
    <td>{{ cart.original_total_price | money }}</td>
  </tr>
  <tr>
    <td colspan="2">Discount Savings</td>
    <td>{{ cart.total_discount | money }}</td>
  </tr>
  <tr>
    <td colspan="2">Total</td>
    <td>{{ cart.total_price | money }}</td>
  </tr>
</tfoot>

Your cart table should now look like this:

Product Quantity Total
Soccer ball 1 $15.00
Tennis ball 5 $22.50 $25.00 (10% off two or more tennis balls)
Running shoe 1 $25.00 $30.00 ($5 off all shoes)
Subtotal $70.00
Discount savings $7.50
Total $62.50

By adding a few new Liquid objects to your templates, you can help your customers understand how their discounts are calculated.

Other examples

  • The following Liquid example shows the discounts for each line item: ```liquid

{% if item.original_line_price != item.line_price %} {{ item.original_line_price | money }} {% endif %} {{ item.line_price | money }} {% for discount in item.discounts %} {{ discount.title }} {% endfor %}

* See another [discount example](/manual/apps/apps-by-shopify/script-editor/examples/vat-script)
) that includes the Liquid code changes.

Want to discuss this page?

Visit the Shopify Community

Ready to start selling online with Shopify?

Try it free