【问题标题】:Woocommerce to Google Sheets Webhook - Multi-product odersWoocommerce 到 Google Sheets Webhook - 多产品订单
【发布时间】:2021-06-10 07:43:18
【问题描述】:

我正在使用下面的脚本通过 webhook 将 WooCommerce 订单导入 Google 表格。 数据按预期导入,但仅显示每个订单的第一个产品。我将如何修改脚本以包含每个订单的多个产品名称和产品数量?*****

//this is a function that fires when the webapp receives a GET request
function doGet(e) {
    return HtmlService.createHtmlOutput("request received");
}

//this is a function that fires when the webapp receives a POST request
function doPost(e) {

    var myData = JSON.parse([e.postData.contents]);
    var order_number = myData.number;
    var first_name = myData.billing.first_name;
    var last_name = myData.billing.last_name;
    var address = myData.billing.address_1;
    var town = myData.billing.city;
    var postcode = myData.billing.postcode;
    var product_name = myData.line_items[0].name;
    var product_qty = myData.line_items[0].quantity;
    var notes = myData.customer_note;
    var timestamp = new Date();
    var sheet = SpreadsheetApp.getActiveSheet();


    sheet.appendRow([timestamp, order_number, first_name, last_name, address, town, postcode, product_name, product_qty, notes]);
}

谢谢, 本

【问题讨论】:

  • 看你是如何导入e.postData.contents中的数据的?

标签: google-sheets woocommerce webhooks google-sheets-api


【解决方案1】:

希望你一切都好。

您面临的问题是,在多产品订单中,您的 JSON 数据中有多个名为“产品标题”的变量,因此使用您的代码,只会读取第一个产品名称。

解决方案:

在您的代码中,替换您的代码

var product_name = myData.line_items[0].name;

使用此代码

for (var i = 0; i < myData.line_items.length; i++){ 
var product_name = myData.line_items[i].name;
var product_qty = myData.line_items[i].quantity;

quantity = quantity + product_qty;
if(i!=0)
{
 description = description + " || " + product_name + " x " + product_qty;
}
else
{
  description = product_name + " x " + product_qty;
}
}

这会遍历 JSON 表,获取所有产品名称,将它们的数量写在它们前面,然后添加一个小分隔符“||”两种不同的产品之间。 乌斯曼

【讨论】:

    猜你喜欢
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 2023-01-22
    • 2022-01-21
    相关资源
    最近更新 更多