In some environments, it can be helpful to have the ability to run scripts in Shopify’s checkout based on whether the order placed was a preorder, backorder, or other scenario where the item was out of stock when the order was placed.
For example, if you integrate with an outside service to suggest product reorders or solicit reviews for your products, you may want to do things differently for an order that will ship right away versus an order that will ship in three weeks.
Thankfully, this is pretty easy to approximate in Liquid. Simply do something like this from the Settings > Checkout screen in the Order Status Page > Additional Scripts section:
{% if first_time_accessed %}
{% assign has_bo = 0 %}
{% for line_item in checkout.line_items %}
{% if (line_item.variant.inventory_quantity < 0) and (line_item.variant.inventory_management == 'shopify') %}
{% assign has_bo = 1 %}
{% endif %}
{% endfor %}
{% if has_bo == 0 %}
<!-- Script goes here -->
{% endif %}
{% endif %}
The first condition, “if first_time_accessed”, is Shopify magic specific to the Order Status page. It simply indicates that this Liquid template section should only run the first time the customer sees the Order Status screen for that order (on checkout), not any time they stop back to check on their order.
We then loop through the line items and determine if any are backordered, based on whether the item has inventory control enabled (line_item.variant.inventory_management == ‘shopify’) and inventory quantity for the item (line_item.variant.inventory_quantity) is negative.
Note that while this simple script worked well enough for my purposes, there is the possibility of a race condition in a high-volume store in a scenario where, in the moment between an in-stock order being received and the customer seeing the order status (order confirmation) screen, another order is processed taking the inventory below zero. If data integrity in a scenario like that is important, it would be smart to use workflows and order metafields to store the backorder status immediately upon the order being received.
Leave a Reply