Skip to content

Instantly share code, notes, and snippets.

@kevinhughes27
Created April 3, 2014 16:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kevinhughes27/9957625 to your computer and use it in GitHub Desktop.
Save kevinhughes27/9957625 to your computer and use it in GitHub Desktop.
update shopify product inventory with the API
I start by getting the product data from Shopify for the product in question, you might already have this data in your database. Using the Shopify API gem for ruby this looks like:
ruby > product = ShopifyAPI::Product.find(183524354)
which returns the following JSON:
{"body_html":"A Baseball Cap","created_at":"2014-03-14T19:25:19-04:00","handle":"moose","id":183524354,"product_type":"hat","published_at":"2014-03-14T19:25:05-04:00","published_scope":"global","template_suffix":null,"title":"Hat","updated_at":"2014-04-03T12:06:34-04:00","vendor":"kevins_sweet_test_shop","tags":"","variants":[{"barcode":null,"compare_at_price":null,"created_at":"2014-03-14T19:25:19-04:00","fulfillment_service":"manual","grams":0,"id":419761934,"inventory_management":"shopify","inventory_policy":"deny","option1":"Red","option2":null,"option3":null,"position":1,"price":"10.00","product_id":183524354,"requires_shipping":true,"sku":"","taxable":true,"title":"Red","updated_at":"2014-04-03T12:06:34-04:00","inventory_quantity":10,"old_inventory_quantity":10},{"barcode":null,"compare_at_price":null,"created_at":"2014-04-03T12:04:07-04:00","fulfillment_service":"manual","grams":0,"id":419848426,"inventory_management":"shopify","inventory_policy":"deny","option1":"Blue","option2":null,"option3":null,"position":2,"price":"20.00","product_id":183524354,"requires_shipping":true,"sku":"","taxable":true,"title":"Blue","updated_at":"2014-04-03T12:06:34-04:00","inventory_quantity":10,"old_inventory_quantity":10}],"options":[{"id":219536818,"name":"Title","position":1,"product_id":183524354}],"images":[]}
Ruby represents the object like this:
=> #<ShopifyAPI::Product:0x007fdf3a598c30
@attributes=
{"body_html"=>"A Baseball Cap",
"created_at"=>"2014-03-14T19:25:19-04:00",
"handle"=>"moose",
"id"=>183524354,
"product_type"=>"hat",
"published_at"=>"2014-03-14T19:25:05-04:00",
"published_scope"=>"global",
"template_suffix"=>nil,
"title"=>"Hat",
"updated_at"=>"2014-04-03T12:04:39-04:00",
"vendor"=>"kevins_sweet_test_shop",
"tags"=>"",
"variants"=>
[#<ShopifyAPI::Variant:0x007fdf3a5a2780
@attributes=
{"barcode"=>nil,
"compare_at_price"=>nil,
"created_at"=>"2014-03-14T19:25:19-04:00",
"fulfillment_service"=>"manual",
"grams"=>0,
"id"=>419761934,
"inventory_management"=>"shopify",
"inventory_policy"=>"deny",
"option1"=>"Red",
"option2"=>nil,
"option3"=>nil,
"position"=>1,
"price"=>"10.00",
"requires_shipping"=>true,
"sku"=>"",
"taxable"=>true,
"title"=>"Red",
"updated_at"=>"2014-04-03T12:04:25-04:00",
"inventory_quantity"=>20,
"old_inventory_quantity"=>20},
@persisted=true,
@prefix_options={:product_id=>183524354}>,
#<ShopifyAPI::Variant:0x007fdf3a5a03b8
@attributes=
{"barcode"=>nil,
"compare_at_price"=>nil,
"created_at"=>"2014-04-03T12:04:07-04:00",
"fulfillment_service"=>"manual",
"grams"=>0,
"id"=>419848426,
"inventory_management"=>"shopify",
"inventory_policy"=>"deny",
"option1"=>"Blue",
"option2"=>nil,
"option3"=>nil,
"position"=>2,
"price"=>"20.00",
"requires_shipping"=>true,
"sku"=>"",
"taxable"=>true,
"title"=>"Blue",
"updated_at"=>"2014-04-03T12:04:39-04:00",
"inventory_quantity"=>20,
"old_inventory_quantity"=>20},
@persisted=true,
@prefix_options={:product_id=>183524354}>],
"options"=>
[#<ShopifyAPI::Option:0x007fdf3a5a9a30
@attributes=
{"id"=>219536818,
"name"=>"Title",
"position"=>1,
"product_id"=>183524354},
@persisted=true,
@prefix_options={}>],
"images"=>[]},
@persisted=true,
@prefix_options={}>
Now to update the inventory I just update that field in the JSON. I did this using Ruby as follows:
ruby > product.variants[0].inventory_quantity = 10
ruby > product.variants[1].inventory_quantity = 10
Now I can save the object in Ruby which will make a PUT request to "/admin/products/183524354.json"
ruby > product.save
=> true
Here is the body of the PUT request in JSON:
Parameters: {"product"=>{"body_html"=>"A Baseball Cap", "created_at"=>Fri, 14 Mar 2014 19:25:19 -0400, "handle"=>"moose", "id"=>183524354, "product_type"=>"hat", "published_at"=>Fri, 14 Mar 2014 19:25:05 -0400, "published_scope"=>"global", "template_suffix"=>nil, "title"=>"Hat", "updated_at"=>Thu, 03 Apr 2014 12:04:39 -0400, "vendor"=>"kevins_sweet_test_shop", "tags"=>"", "variants"=>[{"barcode"=>nil, "compare_at_price"=>nil, "created_at"=>Fri, 14 Mar 2014 19:25:19 -0400, "fulfillment_service"=>"manual", "grams"=>0, "id"=>419761934, "inventory_management"=>"shopify", "inventory_policy"=>"deny", "option1"=>"Red", "option2"=>nil, "option3"=>nil, "position"=>1, "price"=>"10.00", "requires_shipping"=>true, "sku"=>"", "taxable"=>true, "title"=>"Red", "updated_at"=>Thu, 03 Apr 2014 12:04:25 -0400, "inventory_quantity"=>10, "old_inventory_quantity"=>20}, {"barcode"=>nil, "compare_at_price"=>nil, "created_at"=>Thu, 03 Apr 2014 12:04:07 -0400, "fulfillment_service"=>"manual", "grams"=>0, "id"=>419848426, "inventory_management"=>"shopify", "inventory_policy"=>"deny", "option1"=>"Blue", "option2"=>nil, "option3"=>nil, "position"=>2, "price"=>"20.00", "requires_shipping"=>true, "sku"=>"", "taxable"=>true, "title"=>"Blue", "updated_at"=>Thu, 03 Apr 2014 12:04:39 -0400, "inventory_quantity"=>10, "old_inventory_quantity"=>20}], "options"=>[{"id"=>219536818, "name"=>"Title", "position"=>1, "product_id"=>183524354}], "images"=>[]}, "id"=>"183524354"}
After this request you can refresh the shop and verify that the inventory has been updated or we can refetch the product with the API just for debugging:
ruby > product = ShopifyAPI::Product.find(183524354)
=> #<ShopifyAPI::Product:0x007fdf3b8c51a0
@attributes=
{"body_html"=>"A Baseball Cap",
"created_at"=>"2014-03-14T19:25:19-04:00",
"handle"=>"moose",
"id"=>183524354,
"product_type"=>"hat",
"published_at"=>"2014-03-14T19:25:05-04:00",
"published_scope"=>"global",
"template_suffix"=>nil,
"title"=>"Hat",
"updated_at"=>"2014-04-03T12:06:34-04:00",
"vendor"=>"kevins_sweet_test_shop",
"tags"=>"",
"variants"=>
[#<ShopifyAPI::Variant:0x007fdf3b8cf330
@attributes=
{"barcode"=>nil,
"compare_at_price"=>nil,
"created_at"=>"2014-03-14T19:25:19-04:00",
"fulfillment_service"=>"manual",
"grams"=>0,
"id"=>419761934,
"inventory_management"=>"shopify",
"inventory_policy"=>"deny",
"option1"=>"Red",
"option2"=>nil,
"option3"=>nil,
"position"=>1,
"price"=>"10.00",
"requires_shipping"=>true,
"sku"=>"",
"taxable"=>true,
"title"=>"Red",
"updated_at"=>"2014-04-03T12:06:34-04:00",
"inventory_quantity"=>10,
"old_inventory_quantity"=>10},
@persisted=true,
@prefix_options={:product_id=>183524354}>,
#<ShopifyAPI::Variant:0x007fdf3b8cd800
@attributes=
{"barcode"=>nil,
"compare_at_price"=>nil,
"created_at"=>"2014-04-03T12:04:07-04:00",
"fulfillment_service"=>"manual",
"grams"=>0,
"id"=>419848426,
"inventory_management"=>"shopify",
"inventory_policy"=>"deny",
"option1"=>"Blue",
"option2"=>nil,
"option3"=>nil,
"position"=>2,
"price"=>"20.00",
"requires_shipping"=>true,
"sku"=>"",
"taxable"=>true,
"title"=>"Blue",
"updated_at"=>"2014-04-03T12:06:34-04:00",
"inventory_quantity"=>10,
"old_inventory_quantity"=>10},
@persisted=true,
@prefix_options={:product_id=>183524354}>],
"options"=>
[#<ShopifyAPI::Option:0x007fdf3b8d7288
@attributes=
{"id"=>219536818,
"name"=>"Title",
"position"=>1,
"product_id"=>183524354},
@persisted=true,
@prefix_options={}>],
"images"=>[]},
@persisted=true,
@prefix_options={}>
The inventory has been updated to the value of 10 which I set above.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment