I. Workflow:
+ Mục tiêu: cập nhật đơn hàng mà khách mới đặt trên Woocommerce => Google sheet theo thời gian thực
+ Cách làm: sử dụng data cung cấp từ Woocommerce Webhooks và Google Apps script ghi dữ liệu vào Google sheet.
+ Workflow thực tế: Khi Woocommerce nhận được đơn hàng mới => 1 request “post” gửi đến apps script => apps script sẽ turn data nhận được về dạng actual Json object thông qua method Json.parse => ghi các biến được khai báo vào google sheet theo trật tự được khai báo trong scripts.
II. Các bước triển khai chi tiết:
1. Tạo google sheet => tool script editor => google apps script
+ Vào Woocommerce => Nâng cao => Webhook => thêm google apps script url (tạo ở trên vào).
2. Viết Google Apps Script để xử lý dữ liệu gửi từ Woocommerce đến khi có đơn hàng mới:
Tiêu chuẩn để Google sử dụng script như một web app:
+ It contains a doGet(e) or doPost(e) function.
+ The function returns an HTML service HtmlOutput object or a Content service TextOutputobject.
[e.postData.contents]: e is the object of the HTTP call. It will have the data that we’re sending it. So it’s a good idea to check if it’s NULL. If it is, then there’s no need to run the script.
2.1 Cần xem nội dung dữ liệu json từ Woocommerce: Order meta data gồm có:
Order meta data được Woocommerce gửi ra thông qua Webhook
+ công cụ post http request: https://reqbin.com/
+ Cách test order meta data: Sử dụng https://webhook.site – không cần phải đăng ký, chỉ cần tạo webhook trong Woo & nhập delivery Url vào là xong. cách thứ 2 là sử dụng: WooCommerce Logs (WooCommerce > Status > Logs) => Cần enable WP_debug
+ Xử lý raw data trả về theo json format cho dễ nhìn: Json formatter
– Ví dụ về Order meta data (cần xem Order data gửi từ Woo ra để viết apps script tương ứng – dưới đây là ví dụ):
{
"id":1194,
"parent_id":0,
"number":"1194", //đây là ID đơn hàng => apps script: var order_number = myData.number;
"order_key":"wc_order_YzXqv7u79WjZS",
"created_via":"checkout",
"version":"3.6.4",
"status":"on-hold", //tình trạng đơn hàng => var order_status = myData.status;
"currency":"USD",
"date_created":"2019-07-20T02:08:30", //thời gian tạo đơn hàng => apps script: var order_created = myData.date_created;
"date_created_gmt":"2019-07-19T20:38:30",
"date_modified":"2019-07-20T02:08:31",
"date_modified_gmt":"2019-07-19T20:38:31",
"discount_total":"0.00",
"discount_tax":"0.00",
"shipping_total":"200.00",
"shipping_tax":"0.00",
"cart_tax":"0.00",
"total":"4252.00", //tổng giá trị order => var Order_total = myData.total
"total_tax":"0.00",
"prices_include_tax":true,
"customer_id":1,
"customer_ip_address":"::1", //ip khach hang - chua su dung
"customer_user_agent":"Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/75.0.3770.142 Safari\/537.36",
"customer_note":"", //Ghi chu don hang => can dua them vao google sheet
"billing":{
"first_name":"Vishal",
"last_name":"Kothari",
"company":"Tyche Softwares",
"address_1":"B 201, Apsara Building, Rd. 7,", //billing address => chua su dung
"address_2":"Daulat Nagar, Borivali East",
"city":"Mumbai",
"state":"MH",
"postcode":"400066",
"country":"IN",
"email":"vishal@tychesoftwares.com",
"phone":"+919819888007"
},
"shipping":{
"first_name":"Vishal", //ten nguoi nhan - first name
"last_name":"Kothari", //ten nguoi nhan - last name
"company":"Tyche Softwares",
"address_1":"B 201, Apsara Building, Rd. 7,", //dia chi nhan hang 1
"address_2":"Daulat Nagar, Borivali East", //dia chi nhan hang 2
"city":"Mumbai",
"state":"MH",
"postcode":"400066",
"country":"IN"
},
"payment_method":"bacs",
"payment_method_title":"Direct bank transfer", //Phuong thuc thanh toan
"transaction_id":"",
"date_paid":null,
"date_paid_gmt":null,
"date_completed":null,
"date_completed_gmt":null,
"cart_hash":"2774074f472114fd92b2aebe8cb33022",
"meta_data":[
{
"id":16021,
"key":"is_vat_exempt",
"value":"no"
},
{
"id":16023,
"key":"_total_delivery_charges",
"value":"40"
},
{
"id":16024,
"key":"Delivery Date",
"value":"Wednesday, 24 July, 2019"
},
{
"id":16025,
"key":"_orddd_timestamp",
"value":"1563926400"
},
{
"id":16026,
"key":"Time Slot",
"value":"10:30 PM - 10:45 PM"
},
{
"id":16027,
"key":"_orddd_time_slot",
"value":"22:30 - 22:45"
},
{
"id":16028,
"key":"_orddd_timeslot_timestamp",
"value":"1564007400"
}
],
"line_items":[ //line_items[0]
{
"id":463,
"name":"500 GB Hard disk drive", //Tên sản phẩm 1: var product_name = myData.line_items[0].name;
"product_id":375, //id sản phẩm 1 - simple product
"variation_id":0,
"quantity":1, //Số lượng sản phẩm 1: Var myData.line_items[0].quantity
"tax_class":"",
"subtotal":"4000.00",
"subtotal_tax":"0.00",
"total":"4000.00", //Số tiền thanh toán sp 1: Var myData.line_items[0].total
"total_tax":"0.00",
"taxes":[
],
"meta_data":[
],
"sku":"",
"price":4000
},
{
"id":464, //line_item[1]
"name":"Panera Sandwich - Mozzarella",
"product_id":445,
"variation_id":447, //id san pham 2 - varation product
"quantity":1, //so luong san pham 2
"tax_class":"",
"subtotal":"12.00",
"subtotal_tax":"0.00",
"total":"12.00",
"total_tax":"0.00",
"taxes":[
],
"meta_data":[
{
"id":3678,
"key":"select-your-cheese",
"value":"Mozzarella"
}
],
"sku":"",
"price":12
}
],
"tax_lines":[
],
"shipping_lines":[
{
"id":466,
"method_title":"Flat rate",
"method_id":"flat_rate",
"instance_id":"3",
"total":"200.00",
"total_tax":"0.00",
"taxes":[
],
"meta_data":[
{
"id":3690,
"key":"Items",
"value":"500 GB Hard disk drive × 1, Panera Sandwich - Mozzarella × 1"
}
]
}
],
"fee_lines":[
{
"id":465,
"name":"Delivery Charges",
"tax_class":"0",
"tax_status":"taxable",
"amount":"40",
"total":"40.00",
"total_tax":"0.00",
"taxes":[
],
"meta_data":[
]
}
],
"coupon_lines":[
],
"refunds":[
]
}
2.2 Viết apps script tương ứng để thêm những dữ liệu này vào google sheet:
Sau khi hoàn thành apps script => cần publish apps: Deploy as web app => copy “delivery url” và nhập vào Woocommerce webhook.
Chú ý:
+ Chọn mục “Execute the app as me”
+ chọn mục “Who has access to the app: Anyone, even anonymous”
Code mẫu Google apps script để hứng dữ liệu từ Woo Webhook – cần tùy biến dựa trên order data thực tế:
//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, e is the object of the HTTP call. It will have the data that we’re sending it.
function doPost(e) {
var myData = JSON.parse([e.postData.contents]); //Chuyen du lieu nhan duoc sang actual Json object
var order_number = myData.number;
var order_created = myData.date_created;
var order_status = myData.status;
var timestamp = new Date();
var sheet = SpreadsheetApp.getActiveSheet();
sheet.appendRow([timestamp,order_number,order_created,order_status]); //loop through order data to get all products
}
3. Deploying the script as a web app:
Khi thực hiện chỉnh sửa apps script, cần chọn publish as new ở phần “Project version option” thì những sửa đổi mới có hiệu lực.
4. Creating the WooCommerce webhook
Full code dành cho order meta data trên:
//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 order_status = myData.status;
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 payment_method = myData.payment_method_title;
var shipping_method = myData.shipping_lines[0].method_title;
var shipping_total = myData.shipping_lines[0].total;
var timestamp = new Date();
var sheet = SpreadsheetApp.getActiveSheet();
for (i = 0; i < myData.line_items.length; i++) { //loop through all products in the order
sheet.appendRow([timestamp,order_number,order_created,order_status,myData.line_items[i].name]);
//customize and add the detail order to sheet as you want }
}
Reference: https://www.tychesoftwares.com/export-woocommerce-orders-to-google-sheets-in-realtime/
Detail Ref: https://www.freecodecamp.org/news/a-simple-how-to-on-webhooks-the-intimidation-stops-now-9671e8c94c76/
Reference:
+ https://gist.github.com/dexterlabora/af9571cab33f6305851fbf153fb68dcd
+ https://gist.github.com/cpq/6ec1c00379b28551a96bafe14c44315d
+ https://help.babelforce.com/hc/en-us/articles/360015612712-Call-data-to-Google-sheets
+ https://gist.github.com/bmcbride/7069aebd643944c9ee8b – Google Apps Script for POSTING JSON data to a Google Sheet
+ https://github.com/mithunmanohar/woocommerce-orders-google-sheets-integration
+ https://badlywired.com/2018/01/linking-wordpress-to-a-spreadsheet-using-wp-rest-api-and-google-sheets-scripts/
Ref: thisdavej.com/consuming-json-web-data-using-google-sheets/
=> lookup values in multi columns: https://www.ablebits.com/office-addins-blog/2019/12/11/excel-index-match-multiple-criteria-formula-examples/