Google sheets Woocommerce到Google Sheets Webhook-多产品订单

Google sheets Woocommerce到Google Sheets Webhook-多产品订单,google-sheets,woocommerce,webhooks,google-sheets-api,Google Sheets,Woocommerce,Webhooks,Google Sheets Api,我正在使用下面的脚本通过webhook将WooCommerce订单导入Google表单。 数据按预期导入,但仅显示每个订单的第一个产品。如何修改脚本以包含每个订单的多个产品名称和产品数量***** //this is a function that fires when the webapp receives a GET request function doGet(e) { return HtmlService.createHtmlOutput("request receiv

我正在使用下面的脚本通过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]);
}

谢谢,

我希望你做得很好

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

解决方案:

在代码中,替换代码

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;
}
}
for(var i=0;i
它遍历JSON表,获取所有产品名称,将它们的数量写在前面,然后在两个不同的产品之间添加一个小分隔符“| |”。
Usman

这取决于您如何在
e.postData.contents
中导入数据?