This is for a local BSA Troop and I was just trying to make it so when orders came through that they came through to a Google sheet in an organized way so the team can help manage the event.
I am also newer to Javascript, I use PHP/HTML5/CSS3 so I’m sure it’s something simple but the logic seems to be escaping me.
There are only 2 products in WooCommerce on the website.
One product has two variations: though the variation is carried through the title:
Tree Pickup Fundraiser - January 2nd, 2022
Tree Pickup Fundraiser - January 9th, 2022
So what I am trying to do is keep one column always the tree pickup and the second column donations. Though not all people will do Tree Pickup and not all people will do donations. Some will do just donations and some will do just tree pickup.
The issue I have found with my following code below is if both items are defined, donations & tree pickup, then it fires and seems to fire just fine. Though when one is not defined it does not work. I’m assuming I need to assign a value to the myData.line_items[1] but I’m not sure, when I tried to I may have done it incorrectly.
var product_name_2 = (myData.line_items[1].name) ? myData.line_items[1].name : ' ';
Or am I missing something else, any and all help is welcome! Apologies, Javascript I am still really new at.
//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 order_created = myData.date_created;
var scout = myData.customer_note;
var street = myData.billing.address_1;
var street_2 = myData.billing.address_2;
var city = myData.billing.city;
var zip_code = myData.billing.postcode;
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 product_name_2 = myData.line_items[1].name;
var product_qty_2 = myData.line_items[1].quantity;
var product_total_2 = myData.line_items[1].total;
if (product_name_2 == "Donation" || typeof product_name_2 == 'undefined')
{
var first = product_name;
var first_qty = product_qty;
var first_total = product_total;
var second = product_name_2;
var second_total = product_total_2;
}
else
{
var first = product_name_2;
var first_qty = product_qty_2;
var first_total = product_total_2;
var second = product_name;
var second_total = product_total;
}
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 phone = myData.billing.phone;
var timestamp = new Date();
var sheet = SpreadsheetApp.getActiveSheet();
sheet.appendRow([timestamp,order_number,order_created,scout,street,street_2,city,zip_code,first,first_qty,first_total,second,second_total,order_total,billing_email,billing_first_name,billing_last_name,phone]);
}```