Useful code snippets

Add days to the order creation date

If you want to calculate a delivery date by adding a certain number of days to date when an order was placed, then you need to use the following trick.

{%- assign three_days_seconds = 3 | times: 24 | times: 60 | times: 60 %}
<DeliveryDate>{{order.created_at | date: "%s" | plus: three_days_seconds | date: "%Y-%m-%d" }}</DeliveryDate>

What does the code do?

The first line calculates the number of seconds contained within 3 days. You might have typed 259200 right away, however, calculating it this way makes it easier to modify to a different number of days, and looks less of a magic number.

The second line converts the order creation date to a Unix timestamp , which is the number of seconds since 1970-01-01 00:00:00, adds the calculated number of seconds within the days, and finally converts the date back to a desired format.

Exclude a specific line item

The recommended way to exclude line items is to use a filter. For example, you can filter out products of a certain vendor, or tagged with a specific tag.

Make sure that the checkbox "Exclude line items not matching the filter" is enabled.

Nevertheless, if you need a more fine-tuned solution to skip a specific line item, then you can use an if statement in the output template code. Let's say you want to exclude a line item named "Donation". You can use the following expression:

{%- if line_item.name != "Donation" -%} ... item details ... {%- endif -%}

For JSON format, you'll need an extra check to control when to output a comma between subsequent elements.

SKU,Title,Price,Quantity
{%- for line_item in order.line_items %}
{%- if line_item.title != "Donation" %}
{{  line_item.sku -}},
{{- line_item.title -}},
{{- line_item.price -}},
{{- line_item.quantity -}}
{%- endif -%}
{%- endfor %}

Get variant options by name

You can use the following code snippet to get variant options by name. The example shows how to get options named Width, Depth, and Height. You can adjust the names to your needs. Please note that option names are case-sensitive (it matters if letters are uppercase or lowercase).

{%- for item in order.line_items %}
  <item>
    <width>{{item.options_with_values | where: "name", "Width" | map: "value" }}</width>
    <depth>{{item.options_with_values | where: "name", "Depth" | map: "value" }}</depth>
    <height>{{item.options_with_values | where: "name", "Height" | map: "value" }}</height>
  </item>
{%- endfor %}

Get line item properties by name

Shopify limits the number of variant options to three. If you need to extend the customization of your products, then you can use line items properties. Apps like Infinite Options or Custom Product Builder use line item properties to store customer personalization choices.

The code snippet extracts the Engraving and Gift Wrapping options from line item properties.

{%- for item in order.line_items %}
  <item>
    <engraving>{{item.options_with_values | where: "name", "Engraving" | map: "value" }}</engraving>
    <wrapping>{{item.properties | where: "name", "Gift Wrapping" | map: "value" }}</wrapping>
  </item>
{%- endfor %}

Payment date

You can get the payment date from order.transactions:

{{ order.transactions | where: "kind", "sale" | where: "status", "success" | map: "created_at" | first | date: "%Y-%m-%d" }}

Price after discount

There is no variable for price after discount available directly, however, you can calculate it from item.price, item.discount_allocations, and item.quantity.

If you want to get price after discount for a single item, then you first need calculate the total discount value allocated to the item from item.discount_allocations

{%- assign total_discount = 0.0 %}
{%- for discount_allocation in item.discount_allocations %}
    {%- assign total_discount = total_discount | plus: discount_allocation.amount %}
{%- endfor %}

Then you can calculate the unit discount value by dividing total_discount by item.quantity

{%- assign unit_discount = total_discount | divided_by: item.quantity | round: 2 %}

Finally, you can substract the calculated unit_discount from item.price to get unit price after discount.

{%- assign unit_price_after_discount = item.price | minus: unit_discount | round: 2%}

Putting the code snippets together to output unit item price per discount:

<items>
    {%- for item in order.line_items %}
    <item>
        {%- assign total_discount = 0.0 %}
        {%- for discount_allocation in item.discount_allocations %}
            {%- assign total_discount = total_discount | plus: discount_allocation.amount %}
        {%- endfor %}
        {%- assign unit_discount = total_discount | divided_by: item.quantity | round: 2 %}
        {%- assign unit_price_after_discount = item.price | minus: unit_discount | round: 2 %}
        <unit_price_after_discount>{{ unit_price_after_discount }}</unit_price_after_discount >
    </item>
    {%- endfor %}
</items>

On the other hand, if you are looking for total item price after discount, then you need to first multiply item.price by item.quantity and then subtract total_discount.

<items>
    {%- for item in order.line_items %}
    <item>
        {%- assign total_discount = 0.0 %}
        {%- for discount_allocation in item.discount_allocations %}
            {%- assign total_discount = total_discount | plus: discount_allocation.amount %}
        {%- endfor %}
        {%- assign total_price_after_discount = item.price | times: item.quantity | minus: total_discount | round: 2 %}
        <total_price_after_discount>{{ total_price_after_discount }}</total_price_after_discount>
    </item>
    {%- endfor %}
</items>

Gift card code

If a customer pays for an order by a gift card then the information is recorded in {{ order.transactions }}. The transaction gateway is then gift_card. You can get the ID and last four characters of the gift card code from a transaction receipt.

{%- assign gift_card_receipt = order.transactions | where: "gateway", "gift_card" | map: "receipt" | first -%}
{{ gift_card_receipt.gift_card_id }}
{{ gift_card_receipt.gift_card_last_characters }}lo

Total weight

If you export all items, then you can just use the {{ order.total_weight }} variable.

However, if you specified a filter, and selected the option to exclude items not matching the filter, then you'll need to calculate the total weight using the following code snippet.

{%- assign total_grams = 0 -%}
{%- for line_item in order.line_items -%}
    {%- assign line_item_total_grams = line_item.grams | times: line_item.quantity -%}
    {%- assign total_grams = total_grams | plus: line_item_total_grams -%}
{%- endfor -%}
{{ total_grams }}

Shipping tax

When it comes to shipping cost, Shopify gives you only the total shipping price in the order.total_shipping_price_set variable which may or may not include taxes depending on the value of order.taxes_included. There is no variable that would hold shipping tax or a corresponding net/gross shipping price. To get the tax amount of the shipping cost, you'll need to calculate it from order.shipping_lines this way:

{%- assign shipping_tax = 0 %}
{%- for shipping_line in order.shipping_lines %}
    {%- for shipping_tax_line in shipping_line.tax_lines %}
        {%- assign shipping_tax = shipping_tax | plus: shipping_tax_line.price %}
    {%- endfor %}
{%- endfor %}

{{ shipping_tax }}

Barcode image

Using Exporteo, you can generate a pick list PDF that includes barcode images which your logistics service can scan to speed up order fulfillment. To embed a barcode image you need to add the following code to the default PDF template:

<td><img src="https://barcode.tec-it.com/barcode.ashx?data={{line_item.variant.barcode}}&code=UPCA"/></td>

As you may notice, it makes use of an external service to generate the barcode image. Nevertheless, the TEC-IT barcode generator is a free service.

Here is an example preview of an order that includes UPC barcodes:

Refunded line items

You can get a list of refunded line items from order.refunds.

{
    "refund_line_items": [
    {%- assign refund_line_items = order.refunds | flat_map: "refund_line_items" -%}
    {%- for refund_line_item in refund_line_items -%}
    {
      "sku": "{{ refund_line_item.line_item.sku }}",
      "quantity": "{{ refund_line_item.quantity }}",
      "restock_type": "{{ refund_line_item.restock_type }}",
      "subtotal": "{{ refund_line_item.subtotal }}"
    }
    {%- if forloop.last == false -%},{% endif %}
    {%- endfor %}]
}

An order may be refunded partially more than once. Each refund has a list of refund_line_items. The code snippet above leverages the flat_map filter to get a single list of refunded line items from all refunds.

Last updated