Why would product variants be missing the inventory_policy field all together?

Solved
elly5
Tourist
4 1 4

From the API docs a product variant object has an `inventory_policy` field with either the value `continue` or `deny` and does not list an `undefined` option

In my store currently, no product variant has this field, and it's `undefined` whereas all other variant fields are present

 

In what situations would there be no inventory_policy on a variant?

 

This is really weird behaviour on only one of my client stores - all other stores i access do not have this behaviour

 

Any help or suggestions for where to debug would be amazing

 

Are there any inventory settings in the store itself that could affect this field being present at all?

 

Thank you!

0 Likes
CalD
Shopify Staff
Shopify Staff
130 15 19

Hey @elly5,

Can you post (or DM me) a link to the product variant on the client store in question?

CalD | Developer Support @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

0 Likes
elly5
Tourist
4 1 4

This is an accepted solution.

Hi @CalD thanks for your response. I actually worked this out in the past couple days -

 

REASON

Overall this is an issue due to deprecation of certain behaviour by Shopify (https://community.shopify.com/c/API-Announcements/Deprecated-Notice-inventory-quantity-and-inventory...)

The client stores without the issue I mentioned were created prior to December 2017. Since then, Shopify stores created at any point after December 2017 have deprecated inventory_policy and inventory_quantity from being on the variant object available on the product template in liquid (for reasons around crawlers finding this inventory info too easily)

Therefore this new store i created in April 2020 did not have the same access to variant.inventory_policy within the liquid variant object on the product template - causing my 'weird' behaviour when simply duplicating functionality from an existing store (created prior 2017) to a new store (created in 2020)

This deprecation affects only the variant object passed to the product template in liquid ('liquid filters' i think u guys call them to differentiate?) and does not affect the API

Makes sense that base Shopify has 'versions' but this is not something that's affected us before, so I'm wondering if the version number is available anywhere in the Admin to see when debugging this type of stuff? And a changelog?

Also tad confusing that this 'Liquid filter' deprecation is listed in 'API announcements' but does not affect the API and is not deprecated on the API so the 'liquid filter' objects should be further differentiated, or am i missing something? Might just be cool to list these in the API docs (https://shopify.dev/docs/themes/liquid/reference/objects/variant#variant-inventory_policy) if there are only minor differences in Liquid filter objects vs API objects

 

SOLUTION

Anyway there is a solution to manually pass these fields for access in JavaScript.

Instead of previously being able to just say variant.inventory_policy all bundled with the variant object (passed for example through bound Option Selector methods), you can set for example a new global variable called variantsInfo that includes each variants set inventory_policy and uses variant.id to map

The solution is actually in this Stack Overflow answer (https://stackoverflow.com/questions/60649005/using-variant-inventory-policy-of-products-in-my-theme-...)

Repeated here:

Add the below code in your product.liquid and access the information via variantsInfo object in JavaScript.

<script>
let variantsInfo = {
    {%- for variant in product.variants -%}     
        {{variant.id}} : "{{ variant.inventory_policy}}",
    {%- endfor -%}
}
</script>

And usage:

Instead of 

if (variant.inventory_policy == 'continue') {

you would now write

if (variantsInfo[variant.id] == 'continue') {

 

So I guess the issue here just comms/documentation around the difference between Shopify's "Liquid Filter" documentation and API documentation causing the main confusion. Since they sometimes match and sometimes don't so it's unclear where to look, and especially unclear where to look for deprecations or Shopify versions - but I may be missing something so let me know

Is it a rare case where the variant object on the product template, does not cleanly match a variant object from the API? Maybe could document as a note here https://shopify.dev/docs/themes/liquid/reference/objects/variant#variant-inventory_policy 

 

To summarise the findings:

1. The API docs for variant do list inventory_policy as continue/deny and never undefined and this is correct

2. The Liquid variant object accessed on the product template has deprecated inventory_policy field and inventory_quantity field from being available in liquid (and therefore JavaScript using this same object)

3. You can still access these fields and make your own variable, or via the API

4. Workaround working in my store now listed above

 

Hope this helps someone, I noticed nobody gave answers over here https://community.shopify.com/c/API-Announcements/Deprecated-Notice-inventory-quantity-and-inventory... so might link this answer there too

 

Cheers