【问题标题】:WooCommerce REST API can't edit productsWooCommerce REST API 无法编辑产品
【发布时间】:2022-07-19 22:45:02
【问题描述】:

我正在尝试使用 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 休息对其进行修改。即使我创建它,它也会忽略我的参数。我正在关注官方 API 文档,我尝试了 v2 和 v3。

【问题讨论】:

    标签: woocommerce-rest-api


    【解决方案1】:

    您不必只发送您正在使用的所有可用字段。而且您不能在请求中使用只读字段。你可以在这里查看哪些字段是只读的https://woocommerce.github.io/woocommerce-rest-api-docs/#product-properties

    您的请求应如下所示

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

    【讨论】:

      猜你喜欢
      • 2021-08-08
      • 1970-01-01
      • 2021-09-21
      • 1970-01-01
      • 2018-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多