What's your biggest current challenge? Have your say in Community Polls along the right column.

Can liquid track changes in inventory levels?

Solved

Can liquid track changes in inventory levels?

The-Gift-Shop
Excursionist
14 1 6

Can liquid be used to identify when inventory levels have changed?

For example:
if inventory was <= 0 and is now > 0

I'd like to use this logic to display certain messages based on the differences between current and previous inventory status

Accepted Solution (1)

PaulNewton
Shopify Partner
7450 657 1565

This is an accepted solution.

No, liquid doesn't have a "detection"/memory like this.

No does shopify have any exposed properties for frontend usage relating to old or new inventory states, only the current inventory quantity.

 

Unfortunately there aren't even properties you can statically compare to if inventory transfers were being used.

https://shopify.dev/api/liquid/objects/variant#variant-inventory_quantity 

There's incoming and incoming_date but NO next_incoming_quantity 

https://shopify.dev/api/liquid/objects/variant#variant-incoming 

https://shopify.dev/api/liquid/objects/variant#variant-next_incoming_date 

 

Frontend cart quantities

The closest you can achieve for the frontend is comparing how many items are in a users cart and the quantity available or if the item is not available. Either through liquid or in combination with javascript, most themes have a "all items are in your cart" behavior often using javascript that can be used as a jumping off point for such logic.

 

Shopify Flow metafields

Another approach is eligible stores using the free shopify-flow app. 

Since there is no native frontend property for the previous inventory quantity of variants merchants could create one using metafield definitions(mfdf) then use shopify flow to update the mfdfs value to the current-quantity minus the orders-quantity.

Create a variant metafield, optionally make unique ones per locations , etc.

Namespace: inventory

Key: prior_inventory_quantity

https://help.shopify.com/en/manual/shopify-flow/reference/triggers/inventory-quantity-changed 

 

In the flow scripts value use {{inventoryQuantityPrior}} that's a variable made available by the trigger inventory-quantity-changed. not the value when creating the mfdf

 

 

PaulNewton_1-1668248449998.png

Figure - shopify flow setup to update the value of variant mfdf inventory.inventoryQuantityPrior

 

In a themes or notifications,etc  use logic similar to the following to access this mfdf for a product with a single variant in liquid. 

{%- assign priorQuantity = product.first_available_variant
.metafields.inventory.inventoryQuantityPrior -%}

Products with multiple variants will need more specific logic and possibly supporting javascript.

 

 

 

 

 

 

Contact paull.newton+shopifyforum@gmail.com for the solutions you need


Save time & money ,Ask Questions The Smart Way


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Thank Paul with a Coffee for more answers or donate to eff.org


View solution in original post

Replies 4 (4)

ExpertRookie
Shopify Partner
1518 249 323

HI @The-Gift-Shop 
Of course you can custom message base on inventory.
To do that, you need to add some custom code for your theme. If you don't know about the code,
I think you need to hire an expert, they will help you make that.

 

- Was my reply helpful? Please Like and Accept Solution to let me know!
- Want to modify or custom changes on store Hire me.
- Feel free to contact me on expert.rookie.team@gmail.com regarding any help.

PaulNewton
Shopify Partner
7450 657 1565

This is an accepted solution.

No, liquid doesn't have a "detection"/memory like this.

No does shopify have any exposed properties for frontend usage relating to old or new inventory states, only the current inventory quantity.

 

Unfortunately there aren't even properties you can statically compare to if inventory transfers were being used.

https://shopify.dev/api/liquid/objects/variant#variant-inventory_quantity 

There's incoming and incoming_date but NO next_incoming_quantity 

https://shopify.dev/api/liquid/objects/variant#variant-incoming 

https://shopify.dev/api/liquid/objects/variant#variant-next_incoming_date 

 

Frontend cart quantities

The closest you can achieve for the frontend is comparing how many items are in a users cart and the quantity available or if the item is not available. Either through liquid or in combination with javascript, most themes have a "all items are in your cart" behavior often using javascript that can be used as a jumping off point for such logic.

 

Shopify Flow metafields

Another approach is eligible stores using the free shopify-flow app. 

Since there is no native frontend property for the previous inventory quantity of variants merchants could create one using metafield definitions(mfdf) then use shopify flow to update the mfdfs value to the current-quantity minus the orders-quantity.

Create a variant metafield, optionally make unique ones per locations , etc.

Namespace: inventory

Key: prior_inventory_quantity

https://help.shopify.com/en/manual/shopify-flow/reference/triggers/inventory-quantity-changed 

 

In the flow scripts value use {{inventoryQuantityPrior}} that's a variable made available by the trigger inventory-quantity-changed. not the value when creating the mfdf

 

 

PaulNewton_1-1668248449998.png

Figure - shopify flow setup to update the value of variant mfdf inventory.inventoryQuantityPrior

 

In a themes or notifications,etc  use logic similar to the following to access this mfdf for a product with a single variant in liquid. 

{%- assign priorQuantity = product.first_available_variant
.metafields.inventory.inventoryQuantityPrior -%}

Products with multiple variants will need more specific logic and possibly supporting javascript.

 

 

 

 

 

 

Contact paull.newton+shopifyforum@gmail.com for the solutions you need


Save time & money ,Ask Questions The Smart Way


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Thank Paul with a Coffee for more answers or donate to eff.org


The-Gift-Shop
Excursionist
14 1 6

Thanks for taking the time to reply, that detailed info is helpful.

Pedro_73
Shopify Partner
30 1 14

Thanks for this post. It's very useful.
Inside Flow there's a template called: "Tag out of stock products for a single location only" , and it just needs to be adjusted to change the “add tag” to “update metafield variant”.

Thanks !