Use a Metafield value as a condition for "variant inventory quantity" Shopify flows

Use a Metafield value as a condition for "variant inventory quantity" Shopify flows

bear42
Shopify Partner
62 0 7

bear42_0-1713042208946.png

How is it possible to use a variant metafield value, as the value for a "Variant quantity" condition, that will trigger a action in shopify flows. 

Replies 15 (15)

paul_n
Shopify Staff
1339 151 308

See other thread. You can use Run code to do the comparison, which makes the condition easy. Or you can reverse the condition, putting metafields in first and then comparing that with the inventory quantity

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.
bear42
Shopify Partner
62 0 7

Now i did this, but somehow it can not handle negative inventory values.
Metafield is type: "Integer"

What to change here, to make it work?

bear42_0-1713448255370.pngbear42_1-1713448350120.png

 

paul_n
Shopify Staff
1339 151 308

Make sure you are also checking the key and namespace for that same metafield or it will match ANY metafield value. 

 

Regarding negative, I'm not sure what you are asking. 

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.
bear42
Shopify Partner
62 0 7

I know, i want it to check ANY metafield value, but it seems like it does not work anyways. 

Regarding negative, it seems to not work or understand this ("inventoryQuantity"= -5, equal to "Metafield Integer value"= -5)

And it cant match that condition.

bear42_0-1713449540275.png

bear42_1-1713449705429.png

 

 

paul_n
Shopify Staff
1339 151 308

The quantity in available is not necessarily the quantity in inventoryQuantity. That said, what type of metafield is that? A text or number? It's possible that it's trying to compare a string with a number and not casting it because of the negative sign

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.
bear42
Shopify Partner
62 0 7

Which condition should i then use for the available quantity, to match the meta field value?
it seems like it is correct setup with "metafields_item.value equal to (productvariant.inventoryquantity", it is just the fact that it dont match to the metafield value when a negative value. 

The metafield is a "Integer", so if we cant use the negative sign for it, then how do we put in negative values correctly, so it matches the condition correctly?

bear42
Shopify Partner
62 0 7

Also it proves that the actuall: productVariant.inventoryQuantity is getting the negative inventory value of example: -5

bear42_1-1713519095517.pngbear42_2-1713519102170.png

 

paul_n
Shopify Staff
1339 151 308

I'm pretty sure I just confirmed through some testing that it's treating that negative metafield as a string and therefor it doesn't match the number. I'll have to check with engineering if it's possible to fix that. 

 

In the meantime, 2 options. you could get the metafield value in a run code step and output it as an integer. That makes your condition simpler. We also have a metafield improvement coming soon that allows access single metafields and preserves their types (right now they are all strings). I also confirmed this second option works as expected.

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.
bear42
Shopify Partner
62 0 7

Great if you are able to fix that, as it does not make sense that this would not work, as it is now.
Please give me eta if you know when this would be fixed.

can show me how you build "option 2", as i am not really sure how to proceed with this?
I really need something that actually works asap 🙂


paul_n
Shopify Staff
1339 151 308

 

 

Code...make sure you change the namespace and key

export default function main(input) {
  // Make sure that the data you return matches the
  // shape & types defined in the output schema.
  const metafield = input.productVariant.metafields.find((metafield) => metafield.namespace === "custom" && metafield.key === "variant_quantity");

  //convert to int if exists, otherwise 0
  const inventory = metafield ? parseInt(metafield.value) : 0;

  return {
    inventory: inventory,
  }
}

 

Input:

query{
  productVariant {
    metafields {
      namespace
      key
      value
    }
  }
}

 

Output:

query{
  productVariant {
    metafields {
      namespace
      key
      value
    }
  }
}
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.
bear42
Shopify Partner
62 0 7

Alright i set this up now, and do i then just keep this condition for it to check the metafield value should match the variant.inventoryQuantity etc?

bear42_1-1713531167485.png

bear42
Shopify Partner
62 0 7

I put it up like this, but i am not sure if it is correct:

bear42_0-1713531871494.png

 

paul_n
Shopify Staff
1339 151 308

The code looks fine. The condition doesn't need to check the metafield any more. Replace that with the output from your run code step and then check that against the productVariant inventory

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.
bear42
Shopify Partner
62 0 7

Is this correctly setup?

bear42_1-1713538411546.png

 

paul_n
Shopify Staff
1339 151 308

Why did you put that in Log output? That will just return a string

 

The only variables that work in log output are liquid variables (like the output from your run code action)

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.