无法通过Google App Script API将Woocommerce订单详细信息整合到Google Sheet中

时间:2018-06-09 17:49:11

标签: woocommerce google-sheets-api

我有一个API会自动将新的订单详细信息从我的Woocommerce商店提取到Google表格中。它似乎工作,但它未能将前7个订单拉入我的谷歌表。它拥有第一个7之后的所有订单。如何将前7个订单拉入表格?

这是我的Google App脚本:

function start_sync() {
  // Followed instructions at https://github.com/mithunmanohar/woocommerce-orders-google-sheets-integration
  var sheet_name = "OrderDetails"
  update_order_5_min(sheet_name)
}


function update_order_5_min(sheet_name) {
  var ck = "ck_ed82fae51e5bafce28dde95224db9c9c4bd36dba";
  var cs = "cs_9a16fd7b641769a65412f336de3e7a928f7e153c";
  var website = "https://www.funtrackdayz.com";

  var now = new Date();
  var website_t ="240";
  var min = website_t * 60
  now.setMinutes(now.getMinutes() - min);
  var n = now.toISOString();

  var surl = website + "/wc-api/v3/orders?consumer_key=" + ck + "&consumer_secret=" + cs + "&status=processing&filter[created_at_min]=" + n //"&after=2016-10-27T10:10:10Z"
  // var surl = website + "/wc-api/v3/orders?consumer_key=" + ck + "&consumer_secret=" + cs + "&status=processing"
  // &filter[created_at_min]=" + n //"&after=2016-10-27T10:10:10Z"
  var url = surl

  var options = {
        "method": "GET",
        "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
        "muteHttpExceptions": true,
    };
  var result = UrlFetchApp.fetch(url, options);
  if (result.getResponseCode() == 200) {
    var params = JSON.parse(result.getContentText());
  }

  var doc = SpreadsheetApp.getActiveSpreadsheet();
  var temp = doc.getSheetByName(sheet_name);
  var consumption = {}
  //"orders"
  arrayLength = params["orders"].length
  for (var i = 0; i < arrayLength; i++) {
    var container = [];
    a = container.push(params["orders"][i]["billing_address"]["first_name"]);
    a = container.push(params["orders"][i]["billing_address"]["last_name"]);
    a = container.push(params["orders"][i]["billing_address"]["address_1"] + ", " + params["orders"][i]["billing_address"]["address_2"]);

    a = container.push("");

    a = container.push(params["orders"][i]["billing_address"]["city"]);
    a = container.push(params["orders"][i]["billing_address"]["state"]);
    a = container.push(params["orders"][i]["billing_address"]["postcode"]);
    a = container.push(params["orders"][i]["billing_address"]["phone"]);
    a = container.push(params["orders"][i]["billing_address"]["email"]);

    a = container.push(params["orders"][i]["total"]); //price
    a = container.push(params["orders"][i]["payment_details"]["method_id"]);
    c = params["orders"][i]["line_items"].length;
    items = "";
    skus="";

    for (var k = 0; k < c; k++) {
      item = params["orders"][i]["line_items"][k]["name"];
      qty = params["orders"][i]["line_items"][k]["quantity"];
      sku = params["orders"][i]["line_items"][k]["sku"];

      meta = ""
      try {
        meta = params["orders"][i]["line_items"][k]["meta"][0]["value"];
        meta = " - " + meta
      } catch (err) {
        meta = ""
      }

      item_f = qty + " x " + item + meta
      items = items + item_f + ",\n"
      skus = skus + ",\n"
    }

    a = container.push(items)
    a = container.push(sku)
    // a = container.push(params["orders"][i]["total_line_items_quantity"]); // Quantity
    a = container.push(params["orders"][i]["order_number"]); //
    a = container.push(params["orders"][i]["note"])
    a = container.push(params["orders"][i]["created_at"]);

    var doc = SpreadsheetApp.getActiveSpreadsheet();
    var temp = doc.getSheetByName(sheet_name);
    temp.appendRow(container);
    removeDuplicates(sheet_name)
  }
}

function removeDuplicates(sheet_name) {
  var doc = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = doc.getSheetByName(sheet_name);
  var data = sheet.getDataRange().getValues();
  var newData = new Array();

  for (i in data) {
    var row = data[i];
    var duplicate = false;
    for (j in newData) {
      if (row.join() == newData[j].join()) {
          duplicate = true;
      }
    }
    if (!duplicate) {
      newData.push(row);
    }
  }
  sheet.clearContents();
  sheet.getRange(1, 1, newData.length, newData[0].length).setValues(newData);
}

0 个答案:

没有答案