WooCommerce REST API可以编辑产品

发布于 2025-01-31 22:49:25 字数 3489 浏览 3 评论 0原文

我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-黛色若梦 2025-02-07 22:49:25

您不必将所有可用的字段仅发送到所使用的字段。而且您不能在请求中使用仅阅读字段。您可以在此处查看哪些字段

{
  "name": "FLUYD SHORTY LADY 2.0MM",
  "type": "variable",
  "sku": "000031",
  "regular_price": "0",
  "manage_stock": true,
  "stock_quantity": 0,
  "attributes": [{
    "id": 5,
    "name": "size",
    "position": 0,
    "visible": true,
    "variation": true,
    "options": ["L", "M", "S", "XL", "XS"]
  }]
}

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

{
  "name": "FLUYD SHORTY LADY 2.0MM",
  "type": "variable",
  "sku": "000031",
  "regular_price": "0",
  "manage_stock": true,
  "stock_quantity": 0,
  "attributes": [{
    "id": 5,
    "name": "size",
    "position": 0,
    "visible": true,
    "variation": true,
    "options": ["L", "M", "S", "XL", "XS"]
  }]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文