Have your say in Community Polls: What was/is your greatest motivation to start your own business?

Re: API Request to change Inventory Policy

Solved

API Request to change Inventory Policy

Khaoz
Excursionist
13 1 3

Hello, I've got a Flow running to Edit some tags when product has variants in-stock/OoS.

But now I wanted to add a change as well to edit ProductVariantInput.inventoryPolicy via API

Request.

 I'm not code-literate enough to make this work, I know something is wrong, but I can't figure out how to get a log or how to fix, so I come to the Community Gurus for assistance.

This is the code I have on the Action.
mutation 
{% for variants_item in product.variants %}
  {
  "input": {
    "inventoryPolicy": "DENY",
  }
}
{% endfor %}
Accepted Solutions (2)
paul_n
Shopify Staff
1433 157 332

This is an accepted solution.

You have the wrong mutation in #3. 

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

Khaoz
Excursionist
13 1 3

This is an accepted solution.

Kids, this is why you shouldn't be coding on the last leg a of 30 hour day.

@paul_n Thank you so much for making this work.

For future readers, this is the final version of the code to affect each variant's inventoryPolicy within a product. For non-coders like me, you don't have to enter the mutation information or variables, BUT you HAVE to select the correct mutation. If you're using GPT to clean your code, don't, it will add lines and change lower case to upper case here and there. Or do, but delete and format any corrections it makes to comply with the template. And all things before or after a colon : should be in quotes "

{
  "productId": "{{ product.id }}",
  "variants": [
    {% for variants_item in product.variants %}
    {
      "id": "{{ variants_item.id }}",
      "inventoryPolicy": "DENY"
    }{% if forloop.last == false %},{% endif %}
    {% endfor %}
  ],
  "allowPartialUpdates": true
}

View solution in original post

Replies 14 (14)

Khaoz
Excursionist
13 1 3

After reading and moving bits here and there, this is as far as I've gotten, but it's not ticking/unticking the Inventory Policy on the Variant.

mutation productVariantsBulkUpdate($ProductID: ID!, $variants: [ProductVariantsBulkInput!]!) {
  productVariantsBulkUpdate(productId: $ProductID, variants: $variants) {
    product {
      id
    }
    userErrors {
      field
      message
    }
  }
}
{
  "ProductID": "{{product.id}}",
  "variants": [
   {% for variants_item in product.variants %}
  {{variants_item.id}}
     "inventoryPolicy": "CONTINUE"
{% endfor %}
  ]
}

 This is the Evaluated Input

mutation productVariantsBulkUpdate($ProductID: ID!, $variants: [ProductVariantsBulkInput!]!) {
  productVariantsBulkUpdate(productId: $ProductID, variants: $variants) {
    product {
      id
    }
    userErrors {
      field
      message
    }
  }
}
{
  "ProductID": "gid://shopify/Product/9653271265593",
  "variants": [
   
  gid://shopify/ProductVariant/49933101334841
     "inventoryPolicy": "CONTINUE"

  gid://shopify/ProductVariant/49933101498681
     "inventoryPolicy": "CONTINUE"

  gid://shopify/ProductVariant/49933101433145
     "inventoryPolicy": "CONTINUE"

  gid://shopify/ProductVariant/49933101465913
     "inventoryPolicy": "CONTINUE"

  gid://shopify/ProductVariant/49933101400377
     "inventoryPolicy": "CONTINUE"

  gid://shopify/ProductVariant/49933101367609
     "inventoryPolicy": "CONTINUE"

  gid://shopify/ProductVariant/49933101629753
     "inventoryPolicy": "CONTINUE"

  gid://shopify/ProductVariant/49933101564217
     "inventoryPolicy": "CONTINUE"

  gid://shopify/ProductVariant/49933101596985
     "inventoryPolicy": "CONTINUE"

  gid://shopify/ProductVariant/49933101531449
     "inventoryPolicy": "CONTINUE"

  ]
}
paul_n
Shopify Staff
1433 157 332

Use Send Admin API request instead. You'll only need the JSON part. In you code, your variant id needs to be wrapped in:

      "id": "gid://shopify/ProductVariant/123",
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.
Khaoz
Excursionist
13 1 3

Thanks Paul, I tried with only the Json part (I think). Since I need this to change the Policy on all Product Variants, I nested them in a For.

But I got this instead

Ran into exception: Mutation had errors: "Variable $input of type ProductVariantInput! was provided invalid value"
 
Now, this is all I have on my Admin API Request
Khaoz_0-1729627376948.png

 



 

[
  {% for variants_item in product.variants %}
  {
    "id": "{{ variants_item.id }}",
    "inventoryPolicy": "CONTINUE"
  }{% if forloop.last == false %},{% endif %}
  {% endfor %}
]​

 

 
paul_n
Shopify Staff
1433 157 332

Your API request needs to conform to the syntax that API accepts, which is not what you have above. The structure was given when you choose that mutation in the action, but you can find it here: https://shopify.dev/docs/api/admin-graphql/2024-04/mutations/productVariantsBulkUpdate

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.
Khaoz
Excursionist
13 1 3
Ok, your post made me realize I hadn't change the Send API Request to productVariantsBulkUpdate
I've ran the following code

 

{
  "variants": [
    {% for variants_item in product.variants %}
    {
      "id": "{{ variants_item.id }}",
      "inventoryPolicy": "DENY"
    }{% if forloop.last == false %},{% endif %}
    {% endfor %}
  ],
  "allowPartialUpdates": true
}​


And it produced this error,
Ran into exception: Mutation had errors: "Variable $productId of type ID! was provided invalid value"

 

 
So at least now, it's trying to do something. But I'm still equally stumped.
paul_n
Shopify Staff
1433 157 332

productId is required and you haven't provided it.

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.
Khaoz
Excursionist
13 1 3

I found how to provide productID, but, it seems I do need to define Variables, I tried to Get product variant data and in Recent Runs it says it was completed. But now it showed this error.

 

Ran into exception: Mutation had errors: "Variable $variants of type [ProductVariantsBulkInput!]! was provided invalid value", "Variable $productId of type ID! was provided invalid value"

Latest version:

 

{
  "product": {
    "productId": "{{ product.id }}"
  },
  "productVariants": [
    {% for variants_item in product.variants %}
    {
      "id": "{{ variants_item.id }}",
      "inventoryPolicy": "CONTINUE"
    }{% if forloop.last == false %},{% endif %}
    {% endfor %}
  ],
  "allowPartialUpdates": true
}

 

paul_n
Shopify Staff
1433 157 332

That is not the syntax. I'm not sure where you got that "product" part from.

 

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.
Khaoz
Excursionist
13 1 3

 

{
  "productId": "{{ product.id }}",
  "Variants": [
    {% for variants_item in product.variants %}
    {
      "id": "{{ variants_item.id }}",
      "inventoryPolicy": "CONTINUE"
    }{% if forloop.last == false %},{% endif %}
    {% endfor %}
  ],
  "allowPartialUpdates": true
}

 

 

Ran into exception: Mutation input evaluated to invalid JSON. Please ensure the input forms valid JSON after Liquid code is run.

 

Thanks for trying to point me in the right direction. This is what came out in the Failed instance. I don't know where else I'm supposed to fix anything.

 

 

mutation productVariantsBulkUpdate($ProductID: ID!, $variants: [ProductVariantsBulkInput!]!) {
 productVariantsBulkUpdate(productId: $productId, variants: $variants) {
  
   inventoryPolicy: "DENY"

   inventoryPolicy: "DENY"

   inventoryPolicy: "DENY"

   inventoryPolicy: "DENY"

   inventoryPolicy: "DENY"

   inventoryPolicy: "DENY"

   inventoryPolicy: "DENY"

   inventoryPolicy: "DENY"

   inventoryPolicy: "DENY"

   inventoryPolicy: "DENY"

  
  userErrors {
      field
      message
    }
}
{
  "productId": gid://shopify/Product/9654355460409,
  "variants": 
  gid://shopify/ProductVariant/49939777814841

  gid://shopify/ProductVariant/49939777847609

  gid://shopify/ProductVariant/49939777880377

  gid://shopify/ProductVariant/49939777913145

  gid://shopify/ProductVariant/49939777945913

  gid://shopify/ProductVariant/49939777651001

  gid://shopify/ProductVariant/49939777683769

  gid://shopify/ProductVariant/49939777716537

  gid://shopify/ProductVariant/49939777749305

  gid://shopify/ProductVariant/49939777782073

        }
    }

 

 

paul_n
Shopify Staff
1433 157 332

You need to be very detail oriented here. That output does not the match the code you provided. Do you have more code somewhere? If so remove it. Also "variants" should not be capitalized.

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.
Khaoz
Excursionist
13 1 3

I added code after output needed variants, let me try removing it, and fixing "Variants"

Khaoz
Excursionist
13 1 3

Khaoz_1-1729693204885.png

Ok, this got weirder for me, there's two types of mutations in the flow, one to DENY, one to CONTINUE, in separate branches so they would never run twice per flow.

Khaoz_0-1729693014791.png

The DENY branch worked correctly, twice.

 

{
  "productId": "{{ product.id }}",
  "variants": [
    {% for variants_item in product.variants %}
    {
      "id": "{{ variants_item.id }}",
      "inventoryPolicy": "DENY"
    }{% if forloop.last == false %},{% endif %}
    {% endfor %}
  ],
  "allowPartialUpdates": true
}

 

 The CONTINUE branch got an error.

Ran into exception: Mutation had errors: "Variable $input of type ProductVariantInput! was provided invalid value"

Just added another Check to see if inventoryPolicy is already at the desired value, Then do nothing, otherwise run mutation. That seems to have gotten it.

 

paul_n
Shopify Staff
1433 157 332

This is an accepted solution.

You have the wrong mutation in #3. 

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.
Khaoz
Excursionist
13 1 3

This is an accepted solution.

Kids, this is why you shouldn't be coding on the last leg a of 30 hour day.

@paul_n Thank you so much for making this work.

For future readers, this is the final version of the code to affect each variant's inventoryPolicy within a product. For non-coders like me, you don't have to enter the mutation information or variables, BUT you HAVE to select the correct mutation. If you're using GPT to clean your code, don't, it will add lines and change lower case to upper case here and there. Or do, but delete and format any corrections it makes to comply with the template. And all things before or after a colon : should be in quotes "

{
  "productId": "{{ product.id }}",
  "variants": [
    {% for variants_item in product.variants %}
    {
      "id": "{{ variants_item.id }}",
      "inventoryPolicy": "DENY"
    }{% if forloop.last == false %},{% endif %}
    {% endfor %}
  ],
  "allowPartialUpdates": true
}