We are seeing some strange behaviour when updating a product (including variants) using the API.
On occasion, when the variants have a price greater than 0 the price gets set to 0.00 even though this value is not present against the price in the request. Is there any logic in the Shopify API which might be setting this? Is there any way to automatically set products to “Draft” if their variants have 0.00 price?
Setting the variants field on the product resource, defines the variants for that product and will remove any variants whose IDs are not included in the array. So,
If there are no variant IDs included in the array, it assumes you want all new variants and creates them.
If there are variant IDs included in the array then the API call will update the product and the corresponding variants, creating new variants where there isn’t a variant ID defined.
The request you have above contains no variant IDs, so every time you run it it’s deleting all of the variants on the product and creating new ones based on the values in the request. Since none of those values have a price, the price is automatically set to 0.
The variants may look the same because you’re creating them with some of the same values as the previous ones but they are all new and the originals are gone. Your problem isn’t that the price of a variant is being set to 0, rather the variant is being replaced by a new very similar one with a price of 0.
I have tried sending my example request to our Shopify sandbox using Postman without the product variant Ids, but I was not able to replicate the behaviour you are describing. The response returned includes the existing variants with the correct prices. Is there some store configuration which controls this?
I should mention we are using version 2021-01 of the API
I would definitely recommend updating to a more recent version of the API since 2021-01 is no longer supported and you’re likely to get unpredictable results using it. I was not able to replicate the behaviour you describe using the most current supported version, 2022-10.
Our docs describe the id field of ProductVariantInput as:
Specifies the product variant to update or create a new variant if absent.The returned value will be exactly what you put in.
Regardless, I would recommend using the less destructive productVariantsBulkUpdate or productVariantUpdate mutations in GraphQL or the Product Variant resource if using REST. These requests only update specific variants and won’t make unexpected changes to other variants not specified.