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.
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.
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).
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.
Payment date
You can get the payment date from order.transactions
:
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
Then you can calculate the unit discount value by dividing total_discount
by item.quantity
Finally, you can substract the calculated unit_discount
from item.price
to get unit price after discount.
Putting the code snippets together to output unit item price per discount:
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
.
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
.
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.
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:
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:
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
.
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