skip to Main Content

So I wrote this code and it is not working as it should, it is pulling data from woocommerce Webhook with a “code.gs” code in GoogleSheets.

Problem is, if var product_name = myData.line_items[1].name; (and [2], [3] and [4]…. and others) does not exist, the code does not work in GoogleSheets…

What i would like to achieve is, when i have two products in an order (myData.line_items[1].name exists, myData.line_items[2].name exists,…) that GoogleSheets would make a new line with that data for each one of the products.

function doGet(e) {
  return HtmlService.createHtmlOutput("request received");
}

function doPost(e) {
  var myData             = JSON.parse([e.postData.contents]);
  var order_number       = myData.number;
  var order_created      = myData.date_created;
  var product_name       = myData.line_items[0].name;
  var product_qty        = myData.line_items[0].quantity;
  var product_total      = myData.line_items[0].total;
  var produktsku         = myData.line_items[0].sku;

  var product_name       = myData.line_items[1].name;
  var product_qty        = myData.line_items[1].quantity;
  var product_total      = myData.line_items[1].total;
  var produktsku         = myData.line_items[1].sku;

  var product_namea       = myData.line_items[2].name;
  var product_qtya        = myData.line_items[2].quantity;
  var product_totala      = myData.line_items[2].total;
  var produktskua         = myData.line_items[2].sku;

  var product_nameb       = myData.line_items[3].name;
  var product_qtyb        = myData.line_items[3].quantity;
  var product_totalb      = myData.line_items[3].total;
  var produktskub         = myData.line_items[3].sku;

  var product_namec       = myData.line_items[4].name;
  var product_qtyc        = myData.line_items[4].quantity;
  var product_totalc      = myData.line_items[4].total;
  var produktskuc         = myData.line_items[4].sku;

  var product_named       = myData.line_items[5].name;
  var product_qtyd        = myData.line_items[5].quantity;
  var product_totald      = myData.line_items[5].total;
  var produktskud         = myData.line_items[5].sku;

  var order_total        = myData.total;
  var billing_email      = myData.billing.email;
  var billing_first_name = myData.billing.first_name;
  var billing_last_name  = myData.billing.last_name;
  var billing_countryshort    = myData.billing.country;
  var payment_method     = myData.payment_method_title;
  var shipping_method    = myData.shipping_lines[0].method_title;
  var shipping_total     = myData.shipping_lines[0].total;
  var shipping_total     = myData.shipping_lines[0].total;
  var klingi = "1";


  var timestamp = new Date();
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.appendRow([timestamp,order_created,order_number,product_name,produktsku,product_qty,product_total,order_total,billing_email,billing_first_name,billing_last_name,payment_method,shipping_method,shipping_total,billing_countryshort]);
  if( produktskua ) {
      sheet.appendRow(["Izdelek 2", "",order_number,product_namea,produktskua,product_qtya,product_totala]);
};
  if( produktskub ) {
      sheet.appendRow(["Izdelek 3", "",order_number,product_nameb,produktskub,product_qtyb,product_totalb]);
};
  if( produktskuc ) {
      sheet.appendRow(["Izdelek 4", "",order_number,product_namec,produktskuc,product_qtyc,product_totalc]);
};

}

Any ideas?

It stops working, even if I wrap it, it works only if value exists…

      if( myData.line_items[1].name ) {
      var product_namea       = myData.line_items[1].name;
      var product_qtya        = myData.line_items[1].quantity;
      var product_totala      = myData.line_items[1].total;
      var produktskua         = myData.line_items[1].sku;
      };

2

Answers


  1. Chosen as BEST ANSWER

    Is solved like this:

        function doPost(e) {
    var myData = JSON.parse([e.postData.contents]);
      var timestamp = new Date();
      var order_created = myData.date_created; 
      var billing_first_name = myData.billing.first_name;
      var billing_phone = myData.billing.phone;
      var billing_email = myData.billing.email;
      var shipping_address = myData.billing.address_1 + myData.billing.address_2;
      var order_total = myData.total;
      var order_number = myData.number;
      var billing_last_name  = myData.billing.last_name;
      var billing_countryshort    = myData.billing.country;
      var payment_method     = myData.payment_method_title;
      var shipping_method    = myData.shipping_lines[0].method_title;
      var shipping_total     = myData.shipping_lines[0].total;
    
    
        var quantity_prvi = myData.line_items[0].quantity;
        var linetotal_prvi = myData.line_items[0].total;
        var produktsku_prvi = myData.line_items[0].sku;
    
    
      var sheet = SpreadsheetApp.getActiveSheet();
      sheet.appendRow([billing_countryshort,timestamp,order_created,"Order",order_number,billing_first_name,billing_last_name,shipping_address,billing_email,billing_phone,produktsku_prvi,quantity_prvi,linetotal_prvi,shipping_total,shipping_method,order_total,payment_method]);
    
      var lineitems=""
      for (i in myData.line_items)
        if(i>0){
      {
        var quantity = myData.line_items[i].quantity;
        var linetotal = myData.line_items[i].total;
        var produktsku = myData.line_items[i].sku;
      var sheet = SpreadsheetApp.getActiveSheet();
      sheet.appendRow([billing_countryshort,timestamp,order_created,"Dodaten produkt",order_number,"","","","","",produktsku,quantity,linetotal]);
      }
        }
    }
    

  2. When assigning your post data to variables, you can use the ternary operator

    This allows you to verify either a certain postData exists, and if not – assign an empty string to the variable in order to prevent problems with Google Sheets.

    Syntax:

    condition ? exprIfTrue : exprIfFalse
    

    Sample:

    var product_namea = (myData.line_items[2].name) ? myData.line_items[2].name : " ";
    

    Also: Be careful with overwriting variable names, in your code you
    have e.g. twice var product_name

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search