Tự động lấy đơn hàng woocommerce vào google sheets (mới nhất)

Thảo luận trong 'Mã nguồn mở Wordpress' bắt đầu bởi admin, 20/2/21.

  1. admin
    Tham gia ngày:
    22/5/13
    Bài viết:
    4,895
    Đã được thích:
    1,198
    Điểm thành tích:
    113
    Giới tính:
    Nam
    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

    xuat-du-lieu-don-hang-woocomerace-vao-google-sheet.jpg

    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.
    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é)

    tao-ban-sao-google-sheet.jpg

    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)

    chinh-sua-tap-lenh-tren-google-sheet.jpg

    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 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_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_citymyData.billing.city;
      var 
    shipping_statemyData.billing.state;
      var 
    shipping_postcodemyData.billing.postcode;
      var 
    timestamp = new Date();
      var 
    sheet SpreadsheetApp.getActiveSheet();

      for (var 
    0myData.line_items.lengthi++) {
        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)
    code-get-data-woocommerce-to-google-sheet.jpg

    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)
    tuy-chon-trien-khai-moi-google-sheet.jpg

    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.

    ma-ung-dung-web-google-sheet.jpg


    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.
    tao-webhook-truyen-du-lieu-vao-google-sheet.jpg

    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.
    luu-webhook.jpg

    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":[
    ]
    }
     
    Cảm ơn đã xem bài:

    Tự động lấy đơn hàng woocommerce vào google sheets (mới nhất)

    shopcon.vn and olalavui like this.
  2. olalavui
    Tham gia ngày:
    21/2/21
    Bài viết:
    2
    Đã được thích:
    0
    Điểm thành tích:
    1
    Tút này ngon đấy bác ợ
     
  3. shopcon.vn
    Tham gia ngày:
    1/4/21
    Bài viết:
    1
    Đã được thích:
    0
    Điểm thành tích:
    1
    Giới tính:
    Nam
    Cảm ơn Admin rất nhiều. Mình đã làm theo và đã hoạt động!
    Tiện cho mình hỏi cách chèn contact form vào file này luôn ạ thì mình làm theo cách nào ạ!
    Chân thành cảm ơn ạ!
     
  4. niithanoi1
    Tham gia ngày:
    16/5/21
    Bài viết:
    1
    Đã được thích:
    0
    Điểm thành tích:
    1
    Giới tính:
    Nam
    Ad ơi. Cho em hỏi. Với sản phẩm có biến thể là size và màu sắc, thì muốn xuất 2 trường này ra như thế nào ạ. Có thể cho em đoạn code đó được không. Thanks!
     


Chủ để tương tự : Tự động
Diễn đàn Tiêu đề Date
Mã nguồn mở Wordpress Countdown timer trong flatsome tự động reset thời gian cuối ngày 7/3/24
Mã nguồn mở Wordpress Xin code tự động tạo user trong function.php wordpress 6/10/23
Mã nguồn mở Wordpress Tự động 301 thêm dấu gạch chéo xẹc '/' cuối url bị thiếu wordpress 24/5/23
Mã nguồn mở Wordpress Disable update one plugin, chặn thông báo update tự động wordpress 8/4/23
Mã nguồn mở Wordpress Tự động thêm nguồn bài viết khi copy trên website như thế nào? 21/6/21