Cannot update image_id for variant

New Member
3 0 0

Hi,

I'm trying to update image_id field for a given variant, the response returned is 200 OK, but the value is not saved. See below.

GET image

curl -v -X GET -H "X-Shopify-Access-Token: [hidden]" -H "Content-Type: application/json" -H "Cache-Control: no-cache" "https://test-7269.myshopify.com/admin/products/6588661575/images/12566761479.json"
*   Trying 23.227.38.70...
* Connected to test-7269.myshopify.com (23.227.38.70) 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
> GET /admin/products/6588661575/images/12566761479.json HTTP/1.1
> Host: test-7269.myshopify.com
> User-Agent: curl/7.43.0
> Accept: */*
> X-Shopify-Access-Token: abb53ca745f965bafa73020badc936e6
> Content-Type: application/json
> Cache-Control: no-cache
>
< HTTP/1.1 200 OK
< Server: nginx
< Date: Mon, 16 May 2016 16:26:58 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: 20424827
< X-XSS-Protection: 1; mode=block; report=/xss-report/e78041b2-7e39-4fe1-a517-10175b59115c?source%5Baction%5D=show&source%5Bcontroller%5D=admin%2Fproduct_images&source%5Bsection%5D=admin
< X-Request-Id: e78041b2-7e39-4fe1-a517-10175b59115c
< 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
{
    "image": {
        "created_at": "2016-05-16T12:23:53-04:00",
        "id": 12566761479,
        "position": 1,
        "product_id": 6588661575,
        "src": "https://cdn.shopify.com/s/files/1/1277/1391/products/uyf8p3.png?v=1463415833",
        "updated_at": "2016-05-16T12:23:53-04:00",
        "variant_ids": [
            20982651015,
            20982651271,
            20982651527,
            20982651655,
            20982651719,
            20982651847
        ]
    }
}%

GET variant

curl -v -X GET -H "X-Shopify-Access-Token: [hidden]" -H "Content-Type: application/json" -H "Cache-Control: no-cache" "https://test-7269.myshopify.com/admin/variants/20982651015.json"
*   Trying 23.227.38.68...
* Connected to test-7269.myshopify.com (23.227.38.68) 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
> GET /admin/variants/20982651015.json HTTP/1.1
> Host: test-7269.myshopify.com
> User-Agent: curl/7.43.0
> Accept: */*
> X-Shopify-Access-Token: abb53ca745f965bafa73020badc936e6
> Content-Type: application/json
> Cache-Control: no-cache
>
< HTTP/1.1 200 OK
< Server: nginx
< Date: Mon, 16 May 2016 16:28:39 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: 20424827
< X-XSS-Protection: 1; mode=block; report=/xss-report/59a31fb1-3c8c-4013-952c-c3d2a406e598?source%5Baction%5D=show&source%5Bcontroller%5D=admin%2Fproduct_variants&source%5Bsection%5D=admin
< X-Request-Id: 59a31fb1-3c8c-4013-952c-c3d2a406e598
< 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-16T12:03:25-04:00",
        "fulfillment_service": "manual",
        "grams": 0,
        "id": 20982651015,
        "image_id": null,
        "inventory_management": null,
        "inventory_policy": "deny",
        "inventory_quantity": 1,
        "old_inventory_quantity": 1,
        "option1": "Women's Crew Premium",
        "option2": "Charcoal",
        "option3": "S",
        "position": 1,
        "price": "25.99",
        "product_id": 6588661575,
        "requires_shipping": true,
        "sku": "9587-428-140",
        "taxable": true,
        "title": "Women's Crew Premium / Charcoal / S",
        "updated_at": "2016-05-16T12:23:54-04:00",
        "weight": 0.0,
        "weight_unit": "kg"
    }
}

PUT variant

curl -v -X PUT -H "X-Shopify-Access-Token:[hidden]" -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
quote>   "variant": {
quote>     "id": 20982651015,
quote>     "image_id": 12566761479
quote>   }
quote> }' "https://test-7269.myshopify.com/admin/variants/20982651015.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/20982651015.json HTTP/1.1
> Host: test-7269.myshopify.com
> User-Agent: curl/7.43.0
> Accept: */*
> X-Shopify-Access-Token: abb53ca745f965bafa73020badc936e6
> 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: Mon, 16 May 2016 16:29:42 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: 20424827
< Location: https://test-7269.myshopify.com/admin/products/6588661575/variants/20982651015
< X-XSS-Protection: 1; mode=block; report=/xss-report/4692ffa7-ae1a-4600-83e8-70db21886b25?source%5Baction%5D=update&source%5Bcontroller%5D=admin%2Fproduct_variants&source%5Bsection%5D=admin
< X-Request-Id: 4692ffa7-ae1a-4600-83e8-70db21886b25
< 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-16T12:03:25-04:00",
        "fulfillment_service": "manual",
        "grams": 0,
        "id": 20982651015,
        "image_id": null,
        "inventory_management": null,
        "inventory_policy": "deny",
        "inventory_quantity": 1,
        "old_inventory_quantity": 1,
        "option1": "Women's Crew Premium",
        "option2": "Charcoal",
        "option3": "S",
        "position": 1,
        "price": "25.99",
        "product_id": 6588661575,
        "requires_shipping": true,
        "sku": "9587-428-140",
        "taxable": true,
        "title": "Women's Crew Premium / Charcoal / S",
        "updated_at": "2016-05-16T12:23:54-04:00",
        "weight": 0.0,
        "weight_unit": "kg"
    }
}

image_id is still null after the PUT request.

Per documentation (see 3rd example), I should be able to update the image_id.
Is there anything that I'm missing?

(Some old topics that have more or less no resolution)

Thanks

0 Likes
Shopify Expert
9973 84 1491

Hey Raz,

I'm curious. Does this just happen on this product or can you add the variant images to others?

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
0 Likes
New Member
3 0 0

Hey Jason,

Thanks for your reply. Not sure if I understand your question correctly, but this happened on many products while building my application. As my setup is a bit complex, I tried to extract the bug so it can be easy to reproduce.

I'm using Ruby on Rails for my app, and for the sake of simplicity, below are the steps to reproduce the bug using shopify_api gem, which is more or less a wrapper of the API. My belief is that the bug is related to the API and not the gem, as the gem passes the correct values to the API requests.

Here is a short version without any debugging output. If would like to see the actual API calls, I created a gist.

>> session = ShopifyAPI::Session.new(marketer_account.shopify_domain, marketer_account.shopify_token)
>> ShopifyAPI::Base.activate_session(session)
>> ActiveResource::Base.logger = Rails.logger
>> product = ShopifyAPI::Product.create(title: "Test product (1 variant, 1 image)")
>> product.options = [{ name: "Color" }]
>> product.variants = [{ id: nil, option1: "Red", price: 1.99 }]
>> product.save
>> # Uploading images for the first time
>> product.images = [{ id: nil, variant_ids: [product.variants.first.id], src: "http://placehold.it/300/ff0000" }]
>> product.save
>> product.variants.first.image_id
12641514311
>> product.images.first.variant_ids
[
    [0] 21090643015
]
>> # Good!
>> # Now replacing images altogether
>> product.images = [{ id: nil, variant_ids: [product.variants.first.id], src: "http://placehold.it/300/990000" }]
>> product.save
>> 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 images one more time
>> product.images = [{ id: nil, variant_ids: [product.variants.first.id], src: "http://placehold.it/300/330000" }]
>> product.save
>> 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

In a nutshell after I replace an image 2 times, the bug occurs and that variant cannot be associated with the image anymore.

My assumption is that someting went wrong with some associations in the back-end, and probably a Shopify employee could better identify the problem. Both products mentioned in my comments are still available or a new product can be created following the steps above.

0 Likes
Shopify Staff (Retired)
Shopify Staff (Retired)
533 0 88

Hey Raz,

I would suggest creating the image first through the ProductImage POST endpoint as described here.

And then associating the image with the varying through the ProductImage PUT endpoint as described here:

PUT /admin/products/#{id}/images/#{id}.json
{
  "image": {
    "id": 850703190,
    "variant_ids": [
      808950810,
      457924702
    ]
  }
}

I would only suggest creating the product image and associating it with a variant in a single call if you are providing an attachment parameter. Since the image has to be loaded from a remote source for your use-case, creating the image first and then associating it with a variant in the second call is the safest method.

0 Likes
New Member
3 0 0

Hi Jamie,

Going with multiple API calls it seems to work, however that increased the number of API calls for each product from 4 to 4 + number of images per product.

Can you elaborate why would be safer to create the image first and then associate with the variant in a second call? By doing it this way I would double the number of API calls for images.

In my current implementation I'm associating variants on ProductImage POST endpoint, and also I'm still assigning variants using the Product PUT endpoint, however I'm removing the image_id from each variant as will be assigned using the ProductImage endpoint. So far it seems to work fine. 

p.s. I still believe there is an issue on how Product PUT endpoint handles image assignment when variant image_id value doesn't match with image variant_ids; solving this internally would potentially save quite some extra API calls

0 Likes
Highlighted
Shopify Partner
285 0 25

We have started to experience the same issue today.

All the time it was working fine, but just from today it again cannot assign image_id to a variant.

The very exact scenario as explained here.

Does anybody else have the same issue happening with variant images?

 

Thanks!

Excelify.io | Bulk Import Export Update Migrate | https://excelify.io
0 Likes