【问题标题】:Woocommerce Product Creation Automation - GAS - apps scriptWoocommerce 产品创建自动化 - GAS - 应用程序脚本
【发布时间】:2022-11-10 02:07:25
【问题描述】:

遇到 GAS 和 WC RESTv2 之间的问题,我试图创建一个具有自定义属性的简单产品,而我能够在没有任何特定参数的情况下这样做,REST api 返回此错误:

错误:

{"code":"rest_invalid_param","message":"无效参数:属性","data":{"status":400,"params":{"attributes":"attributes[0] 不是对象类型。"},"details":{"attributes":{"code":"rest_invalid_type","message":"attributes[0] 不是对象类型。","data":{"param" :"属性[0]"}}}}}


// the product gets CREATED fine using this simple var data structure :
var data = 
        {
        "name" : " TESTING HERE TOO",
        //"sku" : "TESTINGINGING    ",
        "type" : "simple",
        "regular_price" : "1.99",
        "weight" : 10,
        "manage_stock" : true,
        "stock_quantity" : 10,
        "description" : "testing \n"  } ;
var surl = website + "/wp-json/wc/v2/products?consumer_key=" + keys + "&consumer_secret=" + scret;
var options = 
      {
        "method": "POST",
        "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
        "muteHttpExceptions": true,
        "payload": data
      };

``` // -- the product DOES NOT get created using this :
var data = 
        {
        "name" : " TESTING HERE TOO",
        //"sku" : "TESTINGINGING    ",
        "type" : "simple",
        "regular_price" : "1.99",
        "weight" : 10,
        "manage_stock" : true,
        "stock_quantity" : 10,
        "description" : "testing \n"  } ;
"attributes" : [ { "variation" : "false", "options" : "[ANY]", "id" : 0.0, "name" : "Supplier", "position" : 0.0, "visible" :"false"} , { "variation" : "false", "options" : "[ANY]", "id" : 0.0, "name" : "Invoice", "position" :1.0, "visible" :"false"}]

任何机构都可以阐明问题所在?

【问题讨论】:

    标签: api rest woocommerce


    【解决方案1】:

    万一有人在这里绊倒,这是解决方案:

    如果您想知道错误在哪里,就在这里

    变量选项 = { “方法”:“发布”, //"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8", -- 它不像内容类型必须是 JSON 那样工作 “contentType”:“应用程序/json”, “muteHttpExceptions”:是的, "payload": JSON.stringify(data2) -- 只要 JSON 有效负载因此 stringify 一致。 //“有效载荷”:数据2 };

    当您在 GAS 中的数组中有 JSON 时,REST api 调用将理解的唯一方法是您修改 2 件事

    1. “contentType”:“应用程序/json”, 将有效负载作为 json 发送到 rest api WC

    2. “有效载荷”:JSON.stringify(data2) 将 javascript 对象转换为 json 以便 WC rest api 可以理解

      完毕。

    【讨论】:

      猜你喜欢
      • 2021-11-25
      • 2020-10-15
      • 2021-07-17
      • 2013-05-20
      • 2023-03-12
      • 2019-05-24
      • 1970-01-01
      • 2013-07-09
      • 2016-11-14
      相关资源
      最近更新 更多