Việc tự động lấy đơn hàng khi có khách hàng đặt hàng trên website sử dụng plugin woocommerce khá quan trọng, giúp bạn dễ dàng quản lý đơn hàng, copy đơn hàng, liên kết đơn hàng vào các phần mềm hơn khi sử dụng thông qua Google Sheet, vậy làm sao để kết nối woocommerce đơn hàng (order) tự động cập nhật thông tin đơn hàng vào google Sheet ? Demo: Đơn hàng tự động lấy về khi có khách hàng đặt hàng trên website Cách làm: Bước 1 (xử lý trên google sheet): tài file mẫu excel google sheet có sẵn về tại link dưới. https://docs.google.com/spreadsheets/d/1ISnyhxHo4H21C6cuBqi_zjiPwG2_GD_7JFuAeXSQqVk/edit?usp=sharing Tiếp theo: click vào Tệp và chọn Tạo bản sao. Xong bạn sẽ có 1 bản sao trên google driver của chính bạn. (bây giờ sẽ làm việc với bản sao này của bạn nhé) Tại bản sao mới nhân bản về bạn click: Công cụ -> Trình chỉnh sửa tập lệnh. (để viết code truyền dữ liệu từ đơn hàng cho vào Sheet file excel của google) Chèn code sau vào tập lệnh (nếu trong đó chưa có, nếu có rồi thì thôi, vì các bạn copy file của mình mặc định đã có sẵn rồi) PHP: //this is a function that fires when the webapp receives a GET requestfunction doGet(e) { return HtmlService.createHtmlOutput("request received");}//this is a function that fires when the webapp receives a POST requestfunction 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_phone = myData.billing.phone; var billing_first_name = myData.billing.first_name; var billing_last_name = myData.billing.last_name; var billing_company = myData.shipping.company; var shipping_address_1 = myData.shipping.address_1; var shipping_address_2 = myData.shipping.address_2; var shipping_city= myData.billing.city; var shipping_state= myData.billing.state; var shipping_postcode= myData.billing.postcode; var timestamp = new Date(); var sheet = SpreadsheetApp.getActiveSheet(); 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; var product_total = myData.line_items[i].total; sheet.appendRow([order_number,order_created,product_name,product_qty,product_total,order_total,billing_email,billing_phone,billing_first_name,billing_last_name,billing_company,shipping_address_1,shipping_address_2,shipping_city,shipping_state,shipping_postcode,timestamp,order_status]); }} Xong sẽ như hình sau: Mỗi khi chỉnh sửa file này, bạn nhớ ấn lưu lai (1), sau đó ấn Chạy (2) để xem có lỗi gì không nhé. Sau khi có code xong, bạn ấn Triển khai -> Tùy chọn triển khai mới. (để cập nhật lấy mã của google gắn vào web) Tại màn hình Tùy chọn triển khai mới chọn: bạn điền mô tả là gì bất kỳ để dễ quản lý. Và chọn nút -> Triển khai. Thao tác chọn Uy quyền truy cập -> Allow (nếu google sheet yêu cầu) Hoàn thành xong bạn sẽ có mã như hình sau: Hãy copy mã Ứng dụng web lại và làm theo hướng dẫn dưới để add mã này vào website. Bước 2 (xử lý trên website) Truy cập vào admin của web vào các tab sau (để tạo webhook, truyền dữ liệu mỗi khi khách đặt hàng vào google sheet): Woocommece -> Cài đặt -> Nâng cao -> Các Webhook -> Thêm Webhook. Cài đặt giống dưới, URL giao hàng là URL copy được từ mã Ứng dụng web ở trên. Xong ấn Lưu Webhook. Vậy là xong, bạn ra ngoài đặt hàng xem thành quả nhé. Bài viết tham khảo từ: https://www.tychesoftwares.com/export-woocommerce-orders-to-google-sheets-in-realtime/ Các bạn có thể thêm các tùy chọn khác cho thông báo đơn hàng thông qua các biên trả về như sau: HTML: { "id":1194, "parent_id":0, "number":"1194", "order_key":"wc_order_YzXqv7u79WjZS", "created_via":"checkout", "version":"3.6.4", "status":"on-hold", "currency":"USD", "date_created":"2019-07-20T02:08:30", "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", "total_tax":"0.00", "prices_include_tax":true, "customer_id":1, "customer_ip_address":"::1", "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":"", "billing":{ "first_name":"Vishal", "last_name":"Kothari", "company":"Tyche Softwares", "address_1":"B 201, Apsara Building, Rd. 7,", "address_2":"Daulat Nagar, Borivali East", "city":"Mumbai", "state":"MH", "postcode":"400066", "country":"IN", "email":"vishal@tychesoftwares.com", "phone":"+919819888007" }, "shipping":{ "first_name":"Vishal", "last_name":"Kothari", "company":"Tyche Softwares", "address_1":"B 201, Apsara Building, Rd. 7,", "address_2":"Daulat Nagar, Borivali East", "city":"Mumbai", "state":"MH", "postcode":"400066", "country":"IN" }, "payment_method":"bacs", "payment_method_title":"Direct bank transfer", "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":[ { "id":463, "name":"500 GB Hard disk drive", "product_id":375, "variation_id":0, "quantity":1, "tax_class":"", "subtotal":"4000.00", "subtotal_tax":"0.00", "total":"4000.00", "total_tax":"0.00", "taxes":[ ], "meta_data":[ ], "sku":"", "price":4000 }, { "id":464, "name":"Panera Sandwich - Mozzarella", "product_id":445, "variation_id":447, "quantity":1, "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":[ ] }