Skip to content

Instantly share code, notes, and snippets.

@rmarescu
Last active May 18, 2016 04:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmarescu/2843d89d76d86180c6e7248505e02341 to your computer and use it in GitHub Desktop.
Save rmarescu/2843d89d76d86180c6e7248505e02341 to your computer and use it in GitHub Desktop.
Shopify API assign image_id to variant bug
>> session = ShopifyAPI::Session.new(shopify_domain, shopify_token)
>> ShopifyAPI::Base.activate_session(session)
>> ActiveResource::Base.logger = Rails.logger
>> product = ShopifyAPI::Product.create(title: "Test product (1 variant, 1 image)")
POST https://test-7269.myshopify.com:443/admin/products.json
--> 201 Created 1012 (796.9ms)
Request:
{
"product": {
"title": "Test product (1 variant, 1 image)"
}
}
Headers: {"Content-Type"=>"application/json", "User-Agent"=>"ShopifyAPI/4.2.0 ActiveResource/4.0.0 Ruby/2.3.0", "X-Shopify-Access-Token"=>"[hidden]"}
Response:
{
"product": {
"body_html": null,
"created_at": "2016-05-18T00:24:52-04:00",
"handle": "test-product-1-variant-1-image",
"id": 6627721415,
"image": null,
"images": [],
"options": [
{
"id": 7954355655,
"name": "Title",
"position": 1,
"product_id": 6627721415,
"values": [
"Default Title"
]
}
],
"product_type": "",
"published_at": "2016-05-18T00:24:52-04:00",
"published_scope": "global",
"tags": "",
"template_suffix": null,
"title": "Test product (1 variant, 1 image)",
"updated_at": "2016-05-18T00:24:52-04:00",
"variants": [
{
"barcode": null,
"compare_at_price": null,
"created_at": "2016-05-18T00:24:52-04:00",
"fulfillment_service": "manual",
"grams": 0,
"id": 21090642631,
"image_id": null,
"inventory_management": null,
"inventory_policy": "deny",
"inventory_quantity": 1,
"old_inventory_quantity": 1,
"option1": "Default Title",
"option2": null,
"option3": null,
"position": 1,
"price": "0.00",
"product_id": 6627721415,
"requires_shipping": true,
"sku": "",
"taxable": true,
"title": "Default Title",
"updated_at": "2016-05-18T00:24:52-04:00",
"weight": 0.0,
"weight_unit": "kg"
}
],
"vendor": "test"
}
}
#<ShopifyAPI::Product:0x007ff9e97d9ae8 @attributes={"title"=>"Test product (1 variant, 1 image)", "id"=>6627721415, "body_html"=>nil, "vendor"=>"test", "product_type"=>"", "created_at"=>"2016-05-18T00:24:52-04:00", "handle"=>"test-product-1-variant-1-image", "updated_at"=>"2016-05-18T00:24:52-04:00", "published_at"=>"2016-05-18T00:24:52-04:00", "template_suffix"=>nil, "published_scope"=>"global", "tags"=>"", "variants"=>[#<ShopifyAPI::Variant:0x007ff9eb5e3c00 @attributes={"id"=>21090642631, "title"=>"Default Title", "price"=>"0.00", "sku"=>"", "position"=>1, "grams"=>0, "inventory_policy"=>"deny", "compare_at_price"=>nil, "fulfillment_service"=>"manual", "inventory_management"=>nil, "option1"=>"Default Title", "option2"=>nil, "option3"=>nil, "created_at"=>"2016-05-18T00:24:52-04:00", "updated_at"=>"2016-05-18T00:24:52-04:00", "taxable"=>true, "barcode"=>nil, "image_id"=>nil, "inventory_quantity"=>1, "weight"=>0.0, "weight_unit"=>"kg", "old_inventory_quantity"=>1, "requires_shipping"=>true}, @prefix_options={:product_id=>6627721415}, @persisted=true>], "options"=>[#<ShopifyAPI::Option:0x007ff9eb5e24e0 @attributes={"id"=>7954355655, "product_id"=>6627721415, "name"=>"Title", "position"=>1, "values"=>["Default Title"]}, @prefix_options={}, @persisted=true>], "images"=>[], "image"=>nil}, @prefix_options={}, @persisted=true, @remote_errors=nil, @validation_context=nil, @errors=#<ActiveResource::Errors:0x007ff9e97d9750 @base=#<ShopifyAPI::Product:0x007ff9e97d9ae8 ...>, @messages={}>>
>> product.options = [{ name: "Color" }]
[
[0] {
"name" => "Color"
}
]
>> product.variants = [{ id: nil, option1: "Red", price: 1.99 }]
[
[0] {
"id" => nil,
"option1" => "Red",
"price" => 1.99
}
]
>> product.save
PUT https://test-7269.myshopify.com:443/admin/products/6627721415.json
--> 200 OK 982 (458.1ms)
Request:
{
"product": {
"body_html": null,
"created_at": "2016-05-18T00:24:52-04:00",
"handle": "test-product-1-variant-1-image",
"id": 6627721415,
"image": null,
"images": [],
"options": [
{
"name": "Color"
}
],
"product_type": "",
"published_at": "2016-05-18T00:24:52-04:00",
"published_scope": "global",
"tags": "",
"template_suffix": null,
"title": "Test product (1 variant, 1 image)",
"updated_at": "2016-05-18T00:24:52-04:00",
"variants": [
{
"id": null,
"option1": "Red",
"price": 1.99
}
],
"vendor": "test"
}
}
Headers: {"Content-Type"=>"application/json", "User-Agent"=>"ShopifyAPI/4.2.0 ActiveResource/4.0.0 Ruby/2.3.0", "X-Shopify-Access-Token"=>"[hidden]"}
Response:
{
"product": {
"body_html": null,
"created_at": "2016-05-18T00:24:52-04:00",
"handle": "test-product-1-variant-1-image",
"id": 6627721415,
"image": null,
"images": [],
"options": [
{
"id": 7954355655,
"name": "Color",
"position": 1,
"product_id": 6627721415,
"values": [
"Red"
]
}
],
"product_type": "",
"published_at": "2016-05-18T00:24:52-04:00",
"published_scope": "global",
"tags": "",
"template_suffix": null,
"title": "Test product (1 variant, 1 image)",
"updated_at": "2016-05-18T00:24:52-04:00",
"variants": [
{
"barcode": null,
"compare_at_price": null,
"created_at": "2016-05-18T00:24:52-04:00",
"fulfillment_service": "manual",
"grams": 0,
"id": 21090643015,
"image_id": null,
"inventory_management": null,
"inventory_policy": "deny",
"inventory_quantity": 1,
"old_inventory_quantity": 1,
"option1": "Red",
"option2": null,
"option3": null,
"position": 1,
"price": "1.99",
"product_id": 6627721415,
"requires_shipping": true,
"sku": "",
"taxable": true,
"title": "Red",
"updated_at": "2016-05-18T00:24:52-04:00",
"weight": 0.0,
"weight_unit": "kg"
}
],
"vendor": "test"
}
}
true
>> # Uploading image for the first time
>> product.images = [{ id: nil, variant_ids: [product.variants.first.id], src: "http://placehold.it/300/ff0000" }]
[
[0] {
"id" => nil,
"variant_ids" => [
[0] 21090643015
],
"src" => "http://placehold.it/300/ff0000"
}
]
>> product.save
PUT https://test-7269.myshopify.com:443/admin/products/6627721415.json
--> 200 OK 1599 (847.6ms)
Request:
{
"product": {
"body_html": null,
"created_at": "2016-05-18T00:24:52-04:00",
"handle": "test-product-1-variant-1-image",
"id": 6627721415,
"image": null,
"images": [
{
"id": null,
"src": "http://placehold.it/300/ff0000",
"variant_ids": [
21090643015
]
}
],
"options": [
{
"id": 7954355655,
"name": "Color",
"position": 1,
"product_id": 6627721415,
"values": [
"Red"
]
}
],
"product_type": "",
"published_at": "2016-05-18T00:24:52-04:00",
"published_scope": "global",
"tags": "",
"template_suffix": null,
"title": "Test product (1 variant, 1 image)",
"updated_at": "2016-05-18T00:24:52-04:00",
"variants": [
{
"barcode": null,
"compare_at_price": null,
"created_at": "2016-05-18T00:24:52-04:00",
"fulfillment_service": "manual",
"grams": 0,
"id": 21090643015,
"image_id": null,
"inventory_management": null,
"inventory_policy": "deny",
"inventory_quantity": 1,
"old_inventory_quantity": 1,
"option1": "Red",
"option2": null,
"option3": null,
"position": 1,
"price": "1.99",
"requires_shipping": true,
"sku": "",
"taxable": true,
"title": "Red",
"updated_at": "2016-05-18T00:24:52-04:00",
"weight": 0.0,
"weight_unit": "kg"
}
],
"vendor": "test"
}
}
Headers: {"Content-Type"=>"application/json", "User-Agent"=>"ShopifyAPI/4.2.0 ActiveResource/4.0.0 Ruby/2.3.0", "X-Shopify-Access-Token"=>"[hidden]"}
Response:
{
"product": {
"body_html": null,
"created_at": "2016-05-18T00:24:52-04:00",
"handle": "test-product-1-variant-1-image",
"id": 6627721415,
"image": {
"created_at": "2016-05-18T00:24:54-04:00",
"id": 12641514311,
"position": 1,
"product_id": 6627721415,
"src": "https://cdn.shopify.com/s/files/1/1277/1391/products/text_txtsize_28_bg_ff0000_txt_300_C3_97300_w_300_h_300.png?v=1463545494",
"updated_at": "2016-05-18T00:24:54-04:00",
"variant_ids": [
21090643015
]
},
"images": [
{
"created_at": "2016-05-18T00:24:54-04:00",
"id": 12641514311,
"position": 1,
"product_id": 6627721415,
"src": "https://cdn.shopify.com/s/files/1/1277/1391/products/text_txtsize_28_bg_ff0000_txt_300_C3_97300_w_300_h_300.png?v=1463545494",
"updated_at": "2016-05-18T00:24:54-04:00",
"variant_ids": [
21090643015
]
}
],
"options": [
{
"id": 7954355655,
"name": "Color",
"position": 1,
"product_id": 6627721415,
"values": [
"Red"
]
}
],
"product_type": "",
"published_at": "2016-05-18T00:24:52-04:00",
"published_scope": "global",
"tags": "",
"template_suffix": null,
"title": "Test product (1 variant, 1 image)",
"updated_at": "2016-05-18T00:24:54-04:00",
"variants": [
{
"barcode": null,
"compare_at_price": null,
"created_at": "2016-05-18T00:24:52-04:00",
"fulfillment_service": "manual",
"grams": 0,
"id": 21090643015,
"image_id": 12641514311,
"inventory_management": null,
"inventory_policy": "deny",
"inventory_quantity": 1,
"old_inventory_quantity": 1,
"option1": "Red",
"option2": null,
"option3": null,
"position": 1,
"price": "1.99",
"product_id": 6627721415,
"requires_shipping": true,
"sku": "",
"taxable": true,
"title": "Red",
"updated_at": "2016-05-18T00:24:54-04:00",
"weight": 0.0,
"weight_unit": "kg"
}
],
"vendor": "test"
}
}
true
>> product.variants.first.image_id
12641514311
>> product.images.first.variant_ids
[
[0] 21090643015
]
>> # Good!
>> # Now replacing image altogether
>> product.images = [{ id: nil, variant_ids: [product.variants.first.id], src: "http://placehold.it/300/990000" }]
[
[0] {
"id" => nil,
"variant_ids" => [
[0] 21090643015
],
"src" => "http://placehold.it/300/990000"
}
]
>> product.save
PUT https://test-7269.myshopify.com:443/admin/products/6627721415.json
--> 200 OK 1570 (1294.2ms)
Request:
{
"product": {
"body_html": null,
"created_at": "2016-05-18T00:24:52-04:00",
"handle": "test-product-1-variant-1-image",
"id": 6627721415,
"image": {
"created_at": "2016-05-18T00:24:54-04:00",
"id": 12641514311,
"position": 1,
"src": "https://cdn.shopify.com/s/files/1/1277/1391/products/text_txtsize_28_bg_ff0000_txt_300_C3_97300_w_300_h_300.png?v=1463545494",
"updated_at": "2016-05-18T00:24:54-04:00",
"variant_ids": [
21090643015
]
},
"images": [
{
"id": null,
"src": "http://placehold.it/300/990000",
"variant_ids": [
21090643015
]
}
],
"options": [
{
"id": 7954355655,
"name": "Color",
"position": 1,
"product_id": 6627721415,
"values": [
"Red"
]
}
],
"product_type": "",
"published_at": "2016-05-18T00:24:52-04:00",
"published_scope": "global",
"tags": "",
"template_suffix": null,
"title": "Test product (1 variant, 1 image)",
"updated_at": "2016-05-18T00:24:54-04:00",
"variants": [
{
"barcode": null,
"compare_at_price": null,
"created_at": "2016-05-18T00:24:52-04:00",
"fulfillment_service": "manual",
"grams": 0,
"id": 21090643015,
"image_id": 12641514311,
"inventory_management": null,
"inventory_policy": "deny",
"inventory_quantity": 1,
"old_inventory_quantity": 1,
"option1": "Red",
"option2": null,
"option3": null,
"position": 1,
"price": "1.99",
"requires_shipping": true,
"sku": "",
"taxable": true,
"title": "Red",
"updated_at": "2016-05-18T00:24:54-04:00",
"weight": 0.0,
"weight_unit": "kg"
}
],
"vendor": "test"
}
}
Headers: {"Content-Type"=>"application/json", "User-Agent"=>"ShopifyAPI/4.2.0 ActiveResource/4.0.0 Ruby/2.3.0", "X-Shopify-Access-Token"=>"[hidden]"}
Response:
{
"product": {
"body_html": null,
"created_at": "2016-05-18T00:24:52-04:00",
"handle": "test-product-1-variant-1-image",
"id": 6627721415,
"image": {
"created_at": "2016-05-18T00:25:39-04:00",
"id": 12641534727,
"position": 1,
"product_id": 6627721415,
"src": "https://cdn.shopify.com/s/files/1/1277/1391/products/text_txtsize_28_bg_990000_txt_300_C3_97300_w_300_h_300.png?v=1463545539",
"updated_at": "2016-05-18T00:25:39-04:00",
"variant_ids": []
},
"images": [
{
"created_at": "2016-05-18T00:25:39-04:00",
"id": 12641534727,
"position": 1,
"product_id": 6627721415,
"src": "https://cdn.shopify.com/s/files/1/1277/1391/products/text_txtsize_28_bg_990000_txt_300_C3_97300_w_300_h_300.png?v=1463545539",
"updated_at": "2016-05-18T00:25:39-04:00",
"variant_ids": []
}
],
"options": [
{
"id": 7954355655,
"name": "Color",
"position": 1,
"product_id": 6627721415,
"values": [
"Red"
]
}
],
"product_type": "",
"published_at": "2016-05-18T00:24:52-04:00",
"published_scope": "global",
"tags": "",
"template_suffix": null,
"title": "Test product (1 variant, 1 image)",
"updated_at": "2016-05-18T00:25:39-04:00",
"variants": [
{
"barcode": null,
"compare_at_price": null,
"created_at": "2016-05-18T00:24:52-04:00",
"fulfillment_service": "manual",
"grams": 0,
"id": 21090643015,
"image_id": null,
"inventory_management": null,
"inventory_policy": "deny",
"inventory_quantity": 1,
"old_inventory_quantity": 1,
"option1": "Red",
"option2": null,
"option3": null,
"position": 1,
"price": "1.99",
"product_id": 6627721415,
"requires_shipping": true,
"sku": "",
"taxable": true,
"title": "Red",
"updated_at": "2016-05-18T00:25:39-04:00",
"weight": 0.0,
"weight_unit": "kg"
}
],
"vendor": "test"
}
}
true
>> product.variants.first.image_id
nil
>> product.images.first.variant_ids
[]
>> # Why image has no variant_ids? variant_ids have been set on the request. However, at this point I can still assign image_id for variant
>> # Replace image one more time
>> product.images = [{ id: nil, variant_ids: [product.variants.first.id], src: "http://placehold.it/300/330000" }]
[
[0] {
"id" => nil,
"variant_ids" => [
[0] 21090643015
],
"src" => "http://placehold.it/300/330000"
}
]
>> product.save
PUT https://test-7269.myshopify.com:443/admin/products/6627721415.json
--> 200 OK 1592 (1624.9ms)
Request:
{
"product": {
"body_html": null,
"created_at": "2016-05-18T00:24:52-04:00",
"handle": "test-product-1-variant-1-image",
"id": 6627721415,
"image": {
"created_at": "2016-05-18T00:25:39-04:00",
"id": 12641534727,
"position": 1,
"src": "https://cdn.shopify.com/s/files/1/1277/1391/products/text_txtsize_28_bg_990000_txt_300_C3_97300_w_300_h_300.png?v=1463545539",
"updated_at": "2016-05-18T00:25:39-04:00",
"variant_ids": []
},
"images": [
{
"id": null,
"src": "http://placehold.it/300/330000",
"variant_ids": [
21090643015
]
}
],
"options": [
{
"id": 7954355655,
"name": "Color",
"position": 1,
"product_id": 6627721415,
"values": [
"Red"
]
}
],
"product_type": "",
"published_at": "2016-05-18T00:24:52-04:00",
"published_scope": "global",
"tags": "",
"template_suffix": null,
"title": "Test product (1 variant, 1 image)",
"updated_at": "2016-05-18T00:25:39-04:00",
"variants": [
{
"barcode": null,
"compare_at_price": null,
"created_at": "2016-05-18T00:24:52-04:00",
"fulfillment_service": "manual",
"grams": 0,
"id": 21090643015,
"image_id": null,
"inventory_management": null,
"inventory_policy": "deny",
"inventory_quantity": 1,
"old_inventory_quantity": 1,
"option1": "Red",
"option2": null,
"option3": null,
"position": 1,
"price": "1.99",
"requires_shipping": true,
"sku": "",
"taxable": true,
"title": "Red",
"updated_at": "2016-05-18T00:25:39-04:00",
"weight": 0.0,
"weight_unit": "kg"
}
],
"vendor": "test"
}
}
Headers: {"Content-Type"=>"application/json", "User-Agent"=>"ShopifyAPI/4.2.0 ActiveResource/4.0.0 Ruby/2.3.0", "X-Shopify-Access-Token"=>"[hidden]"}
Response:
{
"product": {
"body_html": null,
"created_at": "2016-05-18T00:24:52-04:00",
"handle": "test-product-1-variant-1-image",
"id": 6627721415,
"image": {
"created_at": "2016-05-18T00:26:21-04:00",
"id": 12641553863,
"position": 1,
"product_id": 6627721415,
"src": "https://cdn.shopify.com/s/files/1/1277/1391/products/text_txtsize_28_bg_330000_txt_300_C3_97300_w_300_h_300.png?v=1463545581",
"updated_at": "2016-05-18T00:26:21-04:00",
"variant_ids": [
21090643015
]
},
"images": [
{
"created_at": "2016-05-18T00:26:21-04:00",
"id": 12641553863,
"position": 1,
"product_id": 6627721415,
"src": "https://cdn.shopify.com/s/files/1/1277/1391/products/text_txtsize_28_bg_330000_txt_300_C3_97300_w_300_h_300.png?v=1463545581",
"updated_at": "2016-05-18T00:26:21-04:00",
"variant_ids": [
21090643015
]
}
],
"options": [
{
"id": 7954355655,
"name": "Color",
"position": 1,
"product_id": 6627721415,
"values": [
"Red"
]
}
],
"product_type": "",
"published_at": "2016-05-18T00:24:52-04:00",
"published_scope": "global",
"tags": "",
"template_suffix": null,
"title": "Test product (1 variant, 1 image)",
"updated_at": "2016-05-18T00:26:21-04:00",
"variants": [
{
"barcode": null,
"compare_at_price": null,
"created_at": "2016-05-18T00:24:52-04:00",
"fulfillment_service": "manual",
"grams": 0,
"id": 21090643015,
"image_id": null,
"inventory_management": null,
"inventory_policy": "deny",
"inventory_quantity": 1,
"old_inventory_quantity": 1,
"option1": "Red",
"option2": null,
"option3": null,
"position": 1,
"price": "1.99",
"product_id": 6627721415,
"requires_shipping": true,
"sku": "",
"taxable": true,
"title": "Red",
"updated_at": "2016-05-18T00:26:21-04:00",
"weight": 0.0,
"weight_unit": "kg"
}
],
"vendor": "test"
}
}
true
>> product.variants.first.image_id
nil
>> product.images.first.variant_ids
[
[0] 21090643015
]
>> # Why variant has image_id null, but it's referenced on image variant_ids?
>> # At this point I cannot update image_id for variant anymore... I'm assuming an association went wrong somewhere behind the scenes...
>> product.variants.first.id
21090643015
>> product.images.first.id
12641553863
curl -v -X PUT -H "X-Shopify-Access-Token: [hidden]" -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
"variant": {
"id": 21090643015,
"image_id": 12641553863
}
}' "https://test-7269.myshopify.com/admin/variants/21090643015.json"
* Trying 23.227.38.71...
* Connected to test-7269.myshopify.com (23.227.38.71) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
* Server certificate: *.myshopify.com
* Server certificate: DigiCert SHA2 High Assurance Server CA
* Server certificate: DigiCert High Assurance EV Root CA
> PUT /admin/variants/21090643015.json HTTP/1.1
> Host: test-7269.myshopify.com
> User-Agent: curl/7.43.0
> Accept: */*
> X-Shopify-Access-Token: 5ba0345796c4f1414de7cd1a736b6791
> Content-Type: application/json
> Cache-Control: no-cache
> Content-Length: 73
>
* upload completely sent off: 73 out of 73 bytes
< HTTP/1.1 200 OK
< Server: nginx
< Date: Wed, 18 May 2016 04:36:37 GMT
< Content-Type: application/json; charset=utf-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< Vary: Accept-Encoding
< Vary: Accept-Encoding
< Status: 200 OK
< X-Frame-Options: DENY
< X-ShopId: 12771391
< X-ShardId: 6
< X-Shopify-Shop-Api-Call-Limit: 1/40
< HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT: 1/40
< X-Stats-UserId: 0
< X-Stats-ApiClientId: 1333827
< X-Stats-ApiPermissionId: 20439077
< Location: https://test-7269.myshopify.com/admin/products/6627721415/variants/21090643015
< X-XSS-Protection: 1; mode=block; report=/xss-report/0c0c75f6-e4f1-4b33-a999-19b52dc74de8?source%5Baction%5D=update&source%5Bcontroller%5D=admin%2Fproduct_variants&source%5Bsection%5D=admin
< X-Request-Id: 0c0c75f6-e4f1-4b33-a999-19b52dc74de8
< P3P: CP="NOI DSP COR NID ADMa OPTa OUR NOR"
< X-Dc: chi2
< X-Content-Type-Options: nosniff
<
* Connection #0 to host test-7269.myshopify.com left intact
{
"variant": {
"barcode": null,
"compare_at_price": null,
"created_at": "2016-05-18T00:24:52-04:00",
"fulfillment_service": "manual",
"grams": 0,
"id": 21090643015,
"image_id": null,
"inventory_management": null,
"inventory_policy": "deny",
"inventory_quantity": 1,
"old_inventory_quantity": 1,
"option1": "Red",
"option2": null,
"option3": null,
"position": 1,
"price": "1.99",
"product_id": 6627721415,
"requires_shipping": true,
"sku": "",
"taxable": true,
"title": "Red",
"updated_at": "2016-05-18T00:26:21-04:00",
"weight": 0.0,
"weight_unit": "kg"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment