【问题标题】:Parse Woocommerce Webhook (JSON)解析 Woocommerce Webhook (JSON)
【发布时间】:2016-01-07 18:02:52
【问题描述】:

当产品更新或下订单时,Wommcommerce 会触发 Webhook。我正在尝试使用作为 WebApp 发布的 Google Apps 脚本 (GAS) 来捕获此 Webhook。我最终想要实现的是将收到的订单写入 Google 电子表格。

我无法从这些 webhook 中获取正确(或至少部分)数据。我尝试了不同的方法将数据写入工作表,但我在工作表中发现的唯一内容是“未定义”。老实说,我不知道如何处理 JSON 数据。我的测试函数现在是:

function doPost(e){ 
  var doc =     DocumentApp.openById('myDocId');
  var jstring = Utilities.jsonStringify(e);
  doc.appendParagraph(jstring);
}

当触发 webhook(订单更新)时,我在文档中得到以下输出:

{"parameter":{},"contextPath":"","contentLength":1523,"queryString":null,"parameters":{},"postData":"FileUpload"}

显然根本没有解析,但我尝试的所有方法(例如 getContentText())都不起作用。 Woocommerce 中的 Webhook-Log 告诉我这是发送的:

{"product":{"title":"testprodukt","id":136,"created_at":"2015-10-06T13:15:38Z","updated_at":"2015-10-09T15: 19:04Z","type":"simple","status":"publish","downloadable":true,"virtual":true,"permalink":"http://MYDOMAIN/produkt/testprodukt/","sku":"", "price":"5.00","re​​gular_price":"5.00","sale_price":null,"price_html":"€5,00","taxable":false,"tax_status":"taxable","tax_class" :"","managing_stock":false,"stock_quantity":"","in_stock":true,"backorders_allowed":false,"backordered":false,"sold_individually":false,"purchaseable":true,"featured" :false,"visible":true,"catalog_visibility":"visible","on_sale":false,"product_url":"","button_text":"","weight":null,"dimensions":{"length ":"","width":"","height":"","unit":"cm"},"shipping_required":false,"shipping_taxable":true,"shipping_class":"","shipping_class_id" :null,"描述":"

somedescription

n","short_description":"","re​​views_allowed":false,"average_rating":"0.00","rating_count":0,"related_ids":[],"upsell_ids":[ ],"cross_sell_ids":[],"parent_id":0,"categories":[],"tags":[],"images":[{"id":0,"created_at":"2015-10- 09T15:19:05Z","updated_at":"2015-10-09T15:19:05Z","src":"http://MYDOMAIN/wp-content/plugins/woocommerce/assets/images/placeholder.png","title":"Platzhalter","alt":"Platzhalter"," position":0}],"featured_src":false,"attributes":[],"downloads":[],"download_limit":0,"download_expiry":0,"download_type":"","purchase_note": "","total_sales":7,"variations":[],"parent":[]}}

我正在寻找提示、链接、示例,以及如何从 webhook 中获取数据的正确方向。

【问题讨论】:

  • 您好,您知道在哪里可以从 webhook 中找到一些示例数据吗?我必须实现客户 eshop 的 webhook,所以我不能“创建新产品”或“新订单”并手动测试它
  • @你用过谷歌电子表格库吗?如何为我的 WordPress 网站实现相同的功能?

标签: json google-apps-script google-sheets woocommerce webhooks


【解决方案1】:

感谢 Riel 和 plus.google 的帮助,我想通了。以我的代码为例,或许有帮助:

function doPost(request) {     

var json = request.postData.getDataAsString(); 
var obj = JSON.parse(json);     


// getting some of the Woocommerce data just as an example
// Hook was fired after order.created
var id = obj.order.id;
var orderNumber = obj.order.order_number;
var payMethod = obj.order.payment_details.method_title;    


// write data in a document, not useful, but straightforward for testing   

var doc =     DocumentApp.openById('myDocumentId');
doc.appendParagraph("Id: " + id);
doc.appendParagraph("orderNumber: " + orderNumber);
doc.appendParagraph("payMethod: " + payMethod);

}

结果应如下所示:

编号:171

订单号:171

payMethod: Per Nachnahme

【讨论】:

    【解决方案2】:

    在 e.postData.content 中

    也使用 JSON.parse 而不是 Utilities。

    【讨论】:

    • 谢谢,我想通了!
    猜你喜欢
    • 2016-09-18
    • 2018-02-17
    • 1970-01-01
    • 2020-10-30
    • 2016-10-16
    • 1970-01-01
    • 2020-06-13
    • 2018-04-07
    • 2021-11-25
    相关资源
    最近更新 更多