Search code examples
javawordpresswordpress-rest-apiwoocommerce-rest-api

How to add product images to store using Java to Woocommerce API v2?


I'm using a java wrapper to upload products from a csv to woocommerce rest api v2 using java. I keep getting this error when inserting an image:

Here is my offending input:

{regular_price=9.99, short_description=This is a short description, manage_stock=true, virtual=true, downloadable=true, images={id=0, url=https://redacted.com/assets/postcardicon.png}, stock_status=in_stock, name=Premium Quality, description=Pellentesque habitant morbi tristique senectus et netus, type=simple, sku=1234, stock_quantity=1}

Here is the offending output:

{code=rest_invalid_param, message=Invalid parameter(s): stock_status, images, data={status=400, params={stock_status=stock_status is not one of instock, outofstock, and onbackorder., images=images is not of type array.}, details={stock_status={code=rest_not_in_enum, message=stock_status is not one of instock, outofstock, and onbackorder., data=null}, images={code=rest_invalid_type, message=images is not of type array., data={param=images}}}}}

Here is valid input when omitting images:

{regular_price=9.99, short_description=This is a short description, manage_stock=true, virtual=true, downloadable=true, stock_status=instock, name=Premium Quality, description=Pellentesque habitant morbi tristique senectus et netus, type=simple, sku=1234, stock_quantity=1}

Here is valid output when omitting images:

{id=40625, name=Premium Quality, slug=premium-quality, permalink=https://redacted.com/product/premium-quality/, date_created=2022-08-13T16:22:43, date_created_gmt=2022-08-13T16:22:43, date_modified=2022-08-13T16:22:43, date_modified_gmt=2022-08-13T16:22:43, type=simple, status=publish, featured=false, catalog_visibility=visible, description=Pellentesque habitant morbi tristique senectus et netus, short_description=This is a short description, sku=1234, price=9.99, regular_price=9.99, sale_price=, date_on_sale_from=null, date_on_sale_from_gmt=null, date_on_sale_to=null, date_on_sale_to_gmt=null, on_sale=false, purchasable=true, total_sales=0, virtual=true, downloadable=true, downloads=[], download_limit=-1, download_expiry=-1, external_url=, button_text=, tax_status=taxable, tax_class=, manage_stock=true, stock_quantity=1, in_stock=true, backorders=no, backorders_allowed=false, backordered=false, sold_individually=false, weight=, dimensions={length=, width=, height=}, shipping_required=false, shipping_taxable=false, shipping_class=, shipping_class_id=0, reviews_allowed=true, average_rating=0, rating_count=0, upsell_ids=[], cross_sell_ids=[], parent_id=0, purchase_note=, categories=[], tags=[], images=[{id=0, date_created=2022-08-13T16:22:44, date_created_gmt=2022-08-13T16:22:44, date_modified=2022-08-13T16:22:44, date_modified_gmt=2022-08-13T16:22:44, src=http://redacted.com/wp-content/plugins/woocommerce/assets/images/placeholder.png, name=Placeholder, alt=Placeholder, position=0}], attributes=[], default_attributes=[], variations=[], grouped_products=[], menu_order=0, price_html=<span class="woocommerce-Price-amount amount"><bdi><span class="woocommerce-Price-currencySymbol">&#36;</span>9.99</bdi></span>, related_ids=[], meta_data=[], _links={self=[{href=https://leads.lionpridefinancial.com/wp-json/wc/v2/products/40625}], collection=[{href=https://redacted.com/wp-json/wc/v2/products}]}}

Offending code:

Map<String, Object> productInfo = new HashMap<>();
productInfo.put("name", "Premium Quality");
productInfo.put("type", "simple");
productInfo.put("description", "Pellentesque habitant morbi tristique senectus et netus");
productInfo.put("sku", "1234");
productInfo.put("short_description", "This is a short description");
productInfo.put("virtual", true);
productInfo.put("downloadable", true);
productInfo.put("regular_price", "9.99");
productInfo.put("stock_status", "in_stock");
productInfo.put("stock_quantity", "1");
productInfo.put("manage_stock", true);

productInfo.put("images", new HashMap <String, Object>() {{
        put("id",0);
        put("url","https://redacted.com/assets/postcardicon.png");
        }});
Map<String, Object> product = wooCommerce.create(EndpointBaseType.PRODUCTS.getValue(), productInfo);

    System.out.println(productInfo.toString());
    System.out.println(product.toString());

Java WooCommerce API Wrapper: https://github.com/calatonsystems/wc-api-java

Thank you for your help! :)

shoutout to @icorederman


Solution

  • It requires multi dimensional array

            ArrayList<Map> imagesArray = new ArrayList<Map>();
    
            Map imagesMap = new HashMap<String, Object>() {
                {
                    put("id", "0");
                    put("url",
                            "https://redacted.com/assets/postcardicon.png");
                }
            };
            imagesArray.add(imagesMap);
            productInfo.put("images", imagesArray);