如何使用Java将产品图像添加到Wooccommerce API v2存储



我使用java包装器将产品从csv上传到使用java的wooccommerce rest api v2。我在插入图像时一直收到这个错误:

以下是我的冒犯性输入:

{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}

以下是有问题的输出:

{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}}}}}

以下是省略图像时的有效输入:

{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}

以下是省略图像时的有效输出:

{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}]}}

违规代码:

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包装器:https://github.com/calatonsystems/wc-api-java

谢谢你的帮助!:(

向@icorederman 大喊

它需要多维阵列

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);

相关内容

  • 没有找到相关文章

最新更新