We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

Populate Variant Metafield with Location Specific Inventory Quantity

Solved

Populate Variant Metafield with Location Specific Inventory Quantity

Todd_Baxter
Tourist
7 1 1

Hi,

 

I'm trying to use Shopify Flow to populate inventory directly to a Variant Metafield but only display a specific Shopify location's inventory and not the total. For reference, the Shopify location name is "Ecommerce". I've already setup part of the flow which uses the following steps:

 

1. Start when = Inventory Quantity Changed

2. Check if = Name is equal to Ecommerce

3. Do this = Update product variant metafield

 

The issue I'm having is I'm not sure how to to populate the value field as the Ecommerce location product variant inventory quantity. The closest I've been able to get is using {{productVariant.inventoryQuantity}}, but that it just providing me with the total on hand quantity and not the Ecommerce location on hand quantity.

 

We are attempting to do this so that we can then provide the Storefront API with this locations specific invento

Accepted Solution (1)
paul_n
Shopify Staff
1828 199 435

This is an accepted solution.

There is whitespace there so it's being treated as a string. Try using hyphens to remove whitespace:

{{- inv.available -}}

  

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.

View solution in original post

Replies 12 (12)

paul_n
Shopify Staff
1828 199 435

You need to loop over productVariant.inventoryItem.inventoryLevels like:

 

{% for inv in productVariant.inventoryItem.inventoryLevels %}
{% if inv.location.name == "MY LOCATION NAME" %}{{ inv.available }}{% endif %}
{% endfor %}
Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
Todd_Baxter
Tourist
7 1 1

Hi,

 

I tried using your formula and enter the location name, which is "Ecommerce", but receiving an error message. First, here is the updated formula I used:

 

{% for inv in productVariant.inventoryItem.inventoryLevels %}
{% if inv.location.name == "Ecommerce" %}{{ inv.available }}{% endif %}
{% endfor %}

 

Both the flow value and variant metafield that I want to populate the value to are setup as Integer value.

 

The error message I'm receiving from Shopify Flow says:

 

Got error updating metafield: "Value must be an integer." For value: " 192 "

 

Not sure what the issue might be except that the populating value isn't being represented as a numerical value. Any additional help would be appreciated.

 

paul_n
Shopify Staff
1828 199 435

This is an accepted solution.

There is whitespace there so it's being treated as a string. Try using hyphens to remove whitespace:

{{- inv.available -}}

  

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
KaranBaro
Shopify Partner
1 0 0

How to add this code? I have added this same as you shared but not working for me!
guide please

bidiman
Excursionist
14 0 6

Hello,
I'm also interested on that one and can't make it work.

I would like to populate a specific location to custom.label1  to be used in Gads.


{% for inv in productVariant.inventoryItem.inventoryLevels %}
{% if inv.location.name == "My Location" %}{{ inv.available }}{% endif %}
{% endfor %}

 

Gives mes the following error:

Got error updating metafield: "Value can't be blank." For value: "\n\n\n\n", Type: single_line_text_field

 

Thanks in advance for your reply,

paul_n
Shopify Staff
1828 199 435

Well you are checking for "My Location" as the name. I doubt you have a location named that. You also have newlines as Liquid inserts that. You can remove that by putting the code on one line or by using hyphens. like {{- foo -}} {%- bar -%}

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
bidiman
Excursionist
14 0 6

Hi Paul,

Thank you for your reply. really appreciate it.

To answer your comment:

-I did wrote "My location" here above  but I do have the right location written in my flow.

-I did place all the code on one line. Since then, I don't have error anymore ! And flow task get complete!
but.... no information are populated into the google custom field 1....
What can be the reason?
please see attached my screenshot.

Screenshot 2024-10-23 at 15.01.28.jpg

paul_n
Shopify Staff
1828 199 435

Are you sure you have the right namespace and key and it's a product metafield? Likely you are still outputting a blank in that field. Usually I test this kind of thing with Log Output first to make sure it's outputting what I expect. 

 

In this case, I would add a {{ inv.location.name }} statement before the "if" to output the exact location names. 

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
bidiman
Excursionist
14 0 6

Hey Paul!

You made my day! 

the problem was here: 

I used "Update product metafield"

instead of using: "Update product variant metafield"

Now it's working perfect!

 

Thank you !

bidiman
Excursionist
14 0 6

Hey Paul,

Hey folks,

I would like to request one more time your knowledge as I'm pretty novice in Liquid.

 

Goal of my flow is to write in a custom metafield if at least one of the variant is available at a specific location.

 

1. when product variant inventory quantity changed

2. sum the inventory level from a specific location of that product.

3.if that value >0 then custom.metafield = "Available" else "Not available"

 

To achieve that I think I should base myself on the following code:

 

{% for inv in productVariant.inventoryItem.inventoryLevels %}{% if inv.location.name == "Billy (Brussels)" %}{{ inv.available }}{% endif %}{% endfor %}

{% if {{ inv.available }} > 0 %}
["Available"]
{% else %}
["Not available"]
{% endif %}

 

 

but I think that code is only taking the value of the updated variant and not the sum of all the variant of that product.

I don't know how to implement properly the liquid sum code in the for loop exemple fo sum:

 

{% assign total_quantity = {{ inv.available }} | sum: "{{ inv.available }}" %}
{{ total_quantity }}

 

Thanks in advance for your help.

 

paul_n
Shopify Staff
1828 199 435

You can't just use {{ inv.available }} there because it's going to be the last iteration of the loop. You would need to use {% assign %} in that loop. But I wouldn't handle this in liquid. Put a condition before the action to check if both the location name and available quantity. And then put a different metafield action in each path with simple ["Available"] assuming your metafield is a list of singe line text strings. If it's just text, `Available` should work. 

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
bidiman
Excursionist
14 0 6

Hi Paul,

Thanks for your reply and sorry for my late reply.
I spent some time today to correct my script and I succeed to properly sum the variant of a product from a specific location. and if the total is more than 0 then I'm writing in my metafield that that product is available at that location 🙂

here is the final code used in the flow to update product variant metafield:

{% assign total_quantity = 0 %}

{% for variant in product.variants %}
  {% for inv in variant.inventoryItem.inventoryLevels %}
    {% if inv.location.name == "Billy (Brussels)" %}
      {% assign available = inv.available | default: 0 %}
      {% assign total_quantity = total_quantity | plus: available %}
    {% endif %}
  {% endfor %}
{% endfor %}

{% if total_quantity > 0 %}
  ["Available"]
{% else %}
  ["Not available"]
{% endif %}