Shopify Flow is an ecommerce automation platform that enables you to automate tasks and processes within your store and across your apps.
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
mutation {% for variants_item in product.variants %} { "input": { "inventoryPolicy": "DENY", } } {% endfor %}
Solved! Go to the solution
This is an accepted solution.
You have the wrong mutation in #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
}
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"
]
}
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",
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
[
{% for variants_item in product.variants %}
{
"id": "{{ variants_item.id }}",
"inventoryPolicy": "CONTINUE"
}{% if forloop.last == false %},{% endif %}
{% endfor %}
]
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
{
"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"
productId is required and you haven't provided it.
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
}
That is not the syntax. I'm not sure where you got that "product" part from.
{
"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
}
}
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.
I added code after output needed variants, let me try removing it, and fixing "Variants"
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.
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.
This is an accepted solution.
You have the wrong mutation in #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
}
Hey Community! As the holiday season unfolds, we want to extend heartfelt thanks to a...
By JasonH Dec 6, 2024Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024Hey Community! It’s time to share some appreciation and celebrate what we have accomplis...
By JasonH Nov 14, 2024