Exporteo Help
Search…
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.
1
{%- assign three_days_seconds = 3 | times: 24 | times: 60 | times: 60 %}
2
<DeliveryDate>{{order.created_at | date: "%s" | plus: three_days_seconds | date: "%Y-%m-%d" }}</DeliveryDate>
Copied!
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.
Exclude line items of vendor X
Exclude items tagged with Y
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.
CSV
JSON
XML
1
SKU,Title,Price,Quantity
2
{%- for line_item in order.line_items %}
3
{%- if line_item.title != "Donation" %}
4
{{ line_item.sku -}},
5
{{- line_item.title -}},
6
{{- line_item.price -}},
7
{{- line_item.quantity -}}
8
{%- endif -%}
9
{%- endfor %}
Copied!
1
{
2
"line_items": [
3
{%- for item in order.line_items %}
4
{%- if item.title != "Description" %}
5
{
6
"sku": {{ item.sku | json }},
7
"title": {{ item.title | json }},
8
"price": {{ item.price | json }},
9
"quantity": {{ item.quantity | json }}
10
}
11
{%- assign next_item = order.line_items[forloop.index] -%}
12
{%- if forloop.last == false and next_item.title != "Description" -%}
13
,
14
{%- endif %}
15
{%- endif %}
16
{%- endfor %}
17
]
18
}
Copied!
1
<items>
2
{%- for item in order.line_items %}
3
{%- if item.title != "Description" %}
4
<item>
5
<sku>{{item.sku}}</sku>
6
<title>{{item.title}}</title>
7
<price>{{item.price}}</price>
8
<quantity>{{item.quantity}}</quantity>
9
</item>
10
{%- endif %}
11
{%- endfor %}
12
</items>
Copied!

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).
1
{%- for item in order.line_items %}
2
<item>
3
<width>{{item.options_with_values | where: "name", "Width" | map: "value" }}</width>
4
<depth>{{item.options_with_values | where: "name", "Depth" | map: "value" }}</depth>
5
<height>{{item.options_with_values | where: "name", "Height" | map: "value" }}</height>
6
</item>
7
{%- endfor %}
Copied!

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
1
{%- assign total_discount = 0.0 %}
2
{%- for discount_allocation in item.discount_allocations %}
3
{%- assign total_discount = total_discount | plus: discount_allocation.amount %}
4
{%- endfor %}
Copied!
Then you can calculate the unit discount value by dividing total_discount by item.quantity
1
{%- assign unit_discount = total_discount | divided_by: item.quantity | round: 2 %}
Copied!
Finally, you can substract the calculated unit_discount from item.price to get unit price after discount.
1
{%- assign unit_price_after_discount = item.price | minus: unit_discount | round: 2%}
Copied!
Putting the code snippets together to output unit item price per discount:
1
<items>
2
{%- for item in order.line_items %}
3
<item>
4
{%- assign total_discount = 0.0 %}
5
{%- for discount_allocation in item.discount_allocations %}
6
{%- assign total_discount = total_discount | plus: discount_allocation.amount %}
7
{%- endfor %}
8
{%- assign unit_discount = total_discount | divided_by: item.quantity | round: 2 %}
9
{%- assign unit_price_after_discount = item.price | minus: unit_discount | round: 2 %}
10
<unit_price_after_discount>{{ unit_price_after_discount }}</unit_price_after_discount >
11
</item>
12
{%- endfor %}
13
</items>
Copied!
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.
1
<items>
2
{%- for item in order.line_items %}
3
<item>
4
{%- assign total_discount = 0.0 %}
5
{%- for discount_allocation in item.discount_allocations %}
6
{%- assign total_discount = total_discount | plus: discount_allocation.amount %}
7
{%- endfor %}
8
{%- assign total_price_after_discount = item.price | times: item.quantity | minus: total_discount | round: 2 %}
9
<total_price_after_discount>{{ total_price_after_discount }}</total_price_after_discount>
10
</item>
11
{%- endfor %}
12
</items>
Copied!

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.
1
{%- assign gift_card_receipt = order.transactions | where: "gateway", "gift_card" | map: "receipt" | first -%}
2
{{ gift_card_receipt.gift_card_id }}
3
{{ gift_card_receipt.gift_card_last_characters }}
Copied!

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.
1
{%- assign total_grams = 0 -%}
2
{%- for line_item in order.line_items -%}
3
{%- assign line_item_total_grams = line_item.grams | times: line_item.quantity -%}
4
{%- assign total_grams = total_grams | plus: line_item_total_grams -%}
5
{%- endfor -%}
6
{{ total_grams }}
Copied!
Last modified 4d ago