Updating a product's variant inventory deletes the other variants

Vic_Gupta
New Member
1 0 1

I am doing a PUT /admin/products/#{id}.json

with the json data containing as an example below.
{"product": {"id" : 138738376, { "variants": [ { "id": 808950810, "inventory_quantity": 10}, {"id": 49148385, "inventory_quantity": 20 } ] } }

After executing the PUT call, the variants in the data are updated, but all the other variants get deleted from the product.

Is this normal or am I doing something wrong here?

Replies 6 (6)
awd
Shopify Staff (Retired)
79 0 7

Hello Vic, this is expected behaviour. You can keep existing variants by including them in this request as well.

Adam Doeler, API Team, Developer at Shopify
Nathen_Bowes
Shopify Staff
12 0 1

Hey Vic,

That is a good question. You are definitely on the right track for updating inventory for a product variant, but when using that endpoint and writting it the way you had, you are replacing the variant information that is associated with that product ID as opposed to changing just the one you want. 

If you want to update product variant inventory quantities, I would recommend you take a look at the PUT /admin/products/#{id}.json endpoint from the document below:

https://docs.shopify.com/api/product#update

Essentially you would need to write out all of the variants in the array so that it updates the ones you want to change, but also keeps the other variants that are on that product. 

Hope that points you in the right direction! 

Take care,

Nathen

Chris_Powers
Tourist
4 0 3

So to be clear, if you do not send every variant id in a product in an UPDATE  it DELTES the variant and ALL its data? Does that not seem like a fairly dangerous issue. Once deleted is there anyway to get these products back? Now I used the PUT /admin/products/#{id}.json as you have stated and the OP stated as well. But no where in the documentation that it would delete all the variants. It makes for quite mess for 190 products with 4 variants each. What happened if you have a database of shopify ids and variants, and the product is manually updated or deleted and new variants are created. So due to a manual update an automtic process would delete ALL of that information?

1) Is there a way to get this data back.

2) I think this needs to be added to your API documentation that using a PUT command and not a delete command will delete variants.

Please respond ASAP.

Rgds

Chris

Chris_Powers
Tourist
4 0 3

It seems to me using

PUT /admin/variants/#{id}.json 
would be the safe way to update a single variant at a time. 

So using the below, the #{id} is the variant number? and there is no reference to the product id it is a variant of.

Update a variant's inventory using an adjustment

PUT /admin/variants/#{id}.json
{
  "variant": {
    "id": 808950810,
    "inventory_quantity_adjustment": -5
  }
}

Update a variant's inventory

PUT /admin/variants/#{id}.json
{
  "variant": {
    "id": 808950810,
    "inventory_quantity": 100,
    "old_inventory_quantity": 10
  }
}
Chris_Powers
Tourist
4 0 3

Hi Shayne,

That is the thing, I did not update the product by sending it new variants as in the above example. I tried to update a single variant with a product id and one of its variants that WAS there. It updated the one variant as expected, but it deleted the data for the other variants. Because the remaining calls, calling each produt with one of the variants came back with the variant not found and the data is missing.

So again it looks like if you do not send ALL the variants in the update it deletes the ones you do not  send. Obviously the

{
  "variant": {
    "id": 808950810,
    "inventory_quantity": 100,
    "old_inventory_quantity": 10
  }
}

would be direct to the variant and update it. But it really should be documented somewhere when updating if you do not send all the variants, it will delete the other variants not sent.

 

Rgds

Juan321654
New Member
1 0 0

correct, Shopify has one of the worst documentation in the history of documentations xd