Ad

How Do I Override Shopify Existing Product Image Position With Ruby?

- 1 answer

I am using the Shopify API with Ruby and I am trying to understand the flow of posting a new product image into an existing product into position 1 and overriding\rearranging the position of the current image into position 2.

products = ShopifyAPI::Product.find(123456789)
products.images << ShopifyAPI::Image.new(:src=> "http://website/CF01.jpg", :position=> 1)
products.save

When setting the current image position to 2 it changes the position attribute but not the actual position of the image on the store, even setting the position to nil doesn't work. Do I need to completely remove and re-upload the current image(s) to get the new image in the first position? Should I be trying to access the ShopifyAPI::Image end point instead?

products = ShopifyAPI::Product.find(123456789)
products.images[0].position = 2
products.images << ShopifyAPI::Image.new(:src=> "http://website/CF01.jpg", :position=> 1)
products.save
Ad

Answer

Uploading the image to the product and saving it then changing the position on the first image then the new image and saving again seemed to have worked.

#this is based on a product with 1 image
products = ShopifyAPI::Product.find(id)
products.images << ShopifyAPI::Image.new(:src=> "http://website.com/image.jpg")
products.save
products.images[0].position = 2
products.images[1].position = 1
products.save
Ad
source: stackoverflow.com
Ad