woocommerce / woocommerce

A customizable, open-source ecommerce platform built on WordPress. Build any commerce solution you can imagine.
https://woocommerce.com
9.23k stars 10.74k forks source link

[DOC-BUG] Product Image id unclear behaviour #43647

Closed ppohlitze closed 1 day ago

ppohlitze commented 6 months ago

Link to the Page/Section

https://woocommerce.github.io/woocommerce-rest-api-docs/?shell#product-images-properties

Description of the Suggestion

The id property of a product image isn't marked as read-only in the documentation. Also no additional information on possible constraints of the id is provided. Yet, when trying to create a product with an image, while also providing an id for the image, the creation seems to always fail with status code 400 and "woocommerce_product_invalid_image_id" regardless of the actual id that is provided.

Example request: curl --location '{redacted}/wp-json/wc/v3/products' \ --header 'Content-Type: application/json' \ --header 'Authorization: Basic {redacted}' \ --data '{ "name": "Test Product", "description": "Habanero", "images": [ { "id": 77777, "src": "{redacted}" } ], "status": "draft", "tax_status": "shipping", "tax_class": "Standard", "catalog_visibility": "hidden" }'

Example response: { "code": "woocommerce_product_invalid_image_id", "message": "#77777 is an invalid image ID.", "data": { "status": 400 } }

Based on the information I was able to find, this behaviour seems odd. Since it's not clear whether this behaviour is intended or not there are multiple possible suggestions:

  1. Marking the field as "read-only" and changing the error response to reflect this e.g "Fields marked as read-only shouldn't be included when creating or updating"
  2. Providing more information on what makes an id valid / invalid so as to make it clearer why a request is failing

Reason for the Suggestion

I'm suggesting this because I wasn't able to find sufficient information on this in the docs which left me confused when the API didn't behave as I expected

Additional Context

vedanshujain commented 2 months ago

While we fix, just noting that ID property of a product image is supposed to be a WP's attachment ID. See the media API.