-
Thanks for your sample about createProduct. Now I'm struggle with update image of a product.
The product don't have images, so getImages return null and cannot set or add. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Re: Update an image to productHi @tamanhtp! I can see the issues in your code. Let me help you fix them. Problems in your current code:
Correct solution:// Get the product
Product product = getProductbyID(products, ID);
// Create new product image
ProductImage pdImage = new ProductImage();
pdImage.setSrc("https://lunpetshop.com/wp-content/uploads/2025/03/z6379545848529_082fa96a13d94801416feb84de1e0824.jpg");
// Use addImagesItem() - it handles null lists automatically
product.addImagesItem(pdImage);
// Update the product
productsApi.updateProductById(ID, product); Key points:
If you have the WordPress media library image ID:ProductImage pdImage = new ProductImage();
pdImage.setId(123); // WordPress media library image ID
pdImage.setSrc("https://lunpetshop.com/wp-content/uploads/2025/03/z6379545848529_082fa96a13d94801416feb84de1e0824.jpg");
product.addImagesItem(pdImage); The "not type Integer" error occurs because WooCommerce API expects proper field types when updating product images. Using Let me know if you need any clarification! :-) |
Beta Was this translation helpful? Give feedback.
Re: Update an image to product
Hi @tamanhtp! I can see the issues in your code. Let me help you fix them.
Problems in your current code:
product.getImages()
returnsnull
when no images exist.pdImage.setSrc()
andpdImage.src()
- the second one is a fluent method that returns the object, not a setter.product.images()
instead ofproduct.setImages()
.Correct solution: