WooCommerce REST API可以编辑产品
我正在尝试使用REST API将该产品添加到WooCommerce,但如果我尝试用名称和slug更新它,则该产品将创建,但没有参数。我没有错误,它只是忽略了更新。
我正在使用API V3。
API调用
//bypass ssl validation check globally for whole application.
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
urlWooComApi = ConfigurationManager.AppSettings.Get("urlWooComApi");
string userWooComApi = ConfigurationManager.AppSettings.Get("userWooComApi");
string pwdWooComApi = ConfigurationManager.AppSettings.Get("pwdWooComApi");
//var settings = new JsonSerializerSettings() { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat };
var settings = new JsonSerializerSettings() { DateFormatHandling = DateFormatHandling.IsoDateFormat };
RestRequest req = new RestRequest(Method.POST);
req.RequestFormat = DataFormat.Json;
req.Resource = "products";
req.AddQueryParameter("consumer_key", userWooComApi);
req.AddQueryParameter("consumer_secret", pwdWooComApi);
var jsonProdotti = JsonConvert.SerializeObject(lstProdotti.Take(1), settings);
req.AddParameter("application/json", jsonProdotti, ParameterType.RequestBody);
RestClient client = new RestClient(urlWooComApi);
IRestResponse resp = client.Execute(req);
if (!string.IsNullOrEmpty(resp.ErrorMessage)) throw new Exception($"{resp.ErrorMessage}");
reasons = resp.Content;
if (resp.StatusCode != HttpStatusCode.Created) throw new Exception($"{reasons}");
JsonDeserializer deserializer = new JsonDeserializer();
var Prod = deserializer.Deserialize<wcProduct>(resp);
return Prod;
JSON
[{
"id": 0,
"name": "WILSON REVOLVE 17 BK SPTM€25",
"slug": "WRZ906900 ",
"permalink": null,
"date_created": "\/Date(-62135596800000)\/",
"date_created_gmt": "\/Date(-62135596800000)\/",
"date_modified": "\/Date(-62135596800000)\/",
"date_modified_gmt": "\/Date(-62135596800000)\/",
"type": null,
"status": null,
"featured": false,
"catalog_visibility": null,
"description": "WILSON REVOLVE 17 BK SPTM€25",
"short_description": null,
"sku": "WRZ906900 ",
"price": null,
"regular_price": "200,000000",
"sale_price": null,
"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": false,
"downloadable": false,
"downloads": null,
"download_limit": 0,
"download_expiry": 0,
"external_url": null,
"button_text": null,
"tax_status": null,
"tax_class": null,
"manage_stock": false,
"stock_quantity": 0,
"backorders": null,
"backorders_allowed": false,
"backordered": false,
"low_stock_amount": null,
"sold_individually": false,
"weight": null,
"dimensions": null,
"shipping_required": false,
"shipping_taxable": false,
"shipping_class": null,
"shipping_class_id": 0,
"reviews_allowed": false,
"average_rating": null,
"rating_count": 0,
"upsell_ids": null,
"cross_sell_ids": null,
"parent_id": 0,
"purchase_note": null,
"categories": null,
"tags": null,
"images": null,
"attributes": null,
"default_attributes": null,
"variations": null,
"grouped_products": null,
"menu_order": 0,
"price_html": null,
"related_ids": null,
"meta_data": null,
"stock_status": null,
"_links": null
}]
我尝试更改主题并停用除WooCommerce以外的所有插件,但我仍然得到相同的结果。我可以创建产品,但无法对其进行修改。即使我创建它,它也会忽略我的参数。我正在遵循官方的API文档,并且尝试了V2和V3。
I'm trying to add this product to Woocommerce using REST API, the product gets created but without parameters, if I try to update it with name and slug it does not work. I don't get errors, it just ignore the update.
I'm using API v3.
API call
//bypass ssl validation check globally for whole application.
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
urlWooComApi = ConfigurationManager.AppSettings.Get("urlWooComApi");
string userWooComApi = ConfigurationManager.AppSettings.Get("userWooComApi");
string pwdWooComApi = ConfigurationManager.AppSettings.Get("pwdWooComApi");
//var settings = new JsonSerializerSettings() { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat };
var settings = new JsonSerializerSettings() { DateFormatHandling = DateFormatHandling.IsoDateFormat };
RestRequest req = new RestRequest(Method.POST);
req.RequestFormat = DataFormat.Json;
req.Resource = "products";
req.AddQueryParameter("consumer_key", userWooComApi);
req.AddQueryParameter("consumer_secret", pwdWooComApi);
var jsonProdotti = JsonConvert.SerializeObject(lstProdotti.Take(1), settings);
req.AddParameter("application/json", jsonProdotti, ParameterType.RequestBody);
RestClient client = new RestClient(urlWooComApi);
IRestResponse resp = client.Execute(req);
if (!string.IsNullOrEmpty(resp.ErrorMessage)) throw new Exception(quot;{resp.ErrorMessage}");
reasons = resp.Content;
if (resp.StatusCode != HttpStatusCode.Created) throw new Exception(quot;{reasons}");
JsonDeserializer deserializer = new JsonDeserializer();
var Prod = deserializer.Deserialize<wcProduct>(resp);
return Prod;
JSON
[{
"id": 0,
"name": "WILSON REVOLVE 17 BK SPTM€25",
"slug": "WRZ906900 ",
"permalink": null,
"date_created": "\/Date(-62135596800000)\/",
"date_created_gmt": "\/Date(-62135596800000)\/",
"date_modified": "\/Date(-62135596800000)\/",
"date_modified_gmt": "\/Date(-62135596800000)\/",
"type": null,
"status": null,
"featured": false,
"catalog_visibility": null,
"description": "WILSON REVOLVE 17 BK SPTM€25",
"short_description": null,
"sku": "WRZ906900 ",
"price": null,
"regular_price": "200,000000",
"sale_price": null,
"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": false,
"downloadable": false,
"downloads": null,
"download_limit": 0,
"download_expiry": 0,
"external_url": null,
"button_text": null,
"tax_status": null,
"tax_class": null,
"manage_stock": false,
"stock_quantity": 0,
"backorders": null,
"backorders_allowed": false,
"backordered": false,
"low_stock_amount": null,
"sold_individually": false,
"weight": null,
"dimensions": null,
"shipping_required": false,
"shipping_taxable": false,
"shipping_class": null,
"shipping_class_id": 0,
"reviews_allowed": false,
"average_rating": null,
"rating_count": 0,
"upsell_ids": null,
"cross_sell_ids": null,
"parent_id": 0,
"purchase_note": null,
"categories": null,
"tags": null,
"images": null,
"attributes": null,
"default_attributes": null,
"variations": null,
"grouped_products": null,
"menu_order": 0,
"price_html": null,
"related_ids": null,
"meta_data": null,
"stock_status": null,
"_links": null
}]
I tried changing theme and deactivating all the plugins except WooCommerce but I still get the same result. I can create the product but I’m not able to modify it thourgh API rest. And even when I creat it it ignores my parameters. I’m following the official API documentation and I tried both v2 and v3.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不必将所有可用的字段仅发送到所使用的字段。而且您不能在请求中使用仅阅读字段。您可以在此处查看哪些字段
You don't have to send every available field just the ones that you are using. And you can't use read-only fields in the requests. You can see which fields are read-only here https://woocommerce.github.io/woocommerce-rest-api-docs/#product-properties
Your request should look like this