1
votes

I've reviewed the documentation for adding woocommerce product images at https://woocommerce.github.io/woocommerce-rest-api-docs/?php#product-images-properties but I don't see a property which defines the sort order of the image within that product. Is that something I can specify using the API?

I've found a plugin at https://enviragallery.com/how-to-change-order-of-images-in-wordpress-gallery/ which allows you to do it in the admin area for one wordpress page or post, but I am importing thousands of products and have image order defined by sku in dropbox which I need to communicate to WooCommerce during the import. Where is that information stored in the database? Is there a default sort order by modified date or name of each image?

Thank you

1

1 Answers

1
votes

I did some testing with an existing product which has multiple images. I noticed I could drag and drop the images for a product which actually changed the order of the images on the website. When I checked the network tab during the update of a product I noticed a parameter called product_image_gallery which had a comma separated list of numbers like 41975,41978,41976,41977,. When I changed the image order and updated the product I saw the numbers move around for that parameter like this: 41975,41976,41977,41978,.

At that point I figured I might be able to change the order of images for a product simply by changing the order they are listed in the images array when passing it to the products endpoint in woocommerce (detailed at https://woocommerce.github.io/woocommerce-rest-api-docs/?shell#create-a-product). I tried this and it worked perfectly. So for example this data puts image1 as the 'primary' product image, then image 2 is followed by image 3 as additional images:

"images": [
  {
    "src": "http://example.com/image1.jpg"
  },
  {
    "src": "http://example.com/image2.jpg"
  },
  {
    "src": "http://example.com/image3.jpg"
  }
]

However sending the images in the following order will make image3.jpg be the main product image, followed by image 2 and then 1 as additional images:

"images": [
  {
    "src": "http://example.com/image3.jpg"
  },
  {
    "src": "http://example.com/image2.jpg"
  },
  {
    "src": "http://example.com/image1.jpg"
  }
]

Apparently there is no field which tracks the order of images within a product unless you count the index of the image in the images array you send to either the create product or update product api calls.