Cách gọi sử dụng RESTful API Getfly lấy đơn hàng Woocommerce Wordpress

Thảo luận trong 'Mã nguồn mở Wordpress' bắt đầu bởi admin, 3/3/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
    Bài viết hướng dẫn cách add thêm gọi RESTful API Getfly lấy đơn hàng Woocommerce mỗi khi khách hàng đặt hàng xong, đơn hàng sẽ được chuyển về cho bên Getfly quản lý.

    Việc xử lý các API khác của Getfly tương tự với xử lý đơn hàng Woocommerce, các bạn làm theo hướng dẫn chi tiết để biết cách gọi api Getfly và truyền dữ liệu đi khi có đơn đặt hàng thành công trong sự kiện trang cảm ơn.

    Demo: Mỗi khi tạo đơn hàng xong, bên PM Getfly sẽ tạo 1 User với thông tin bạn truyền vào từ phần đặt hàng.

    api-getfly.jpg

    Và thông tin đơn hàng của bạn sẽ có danh sách và chi tiết đơn hàng như sau:

    don-hang-woocommerce-api-getfly.jpg
    Bước 1: Đầu tiên các bạn phải đọc tài liệu của bên cung cấp dịch vụ API trước link ở dưới của Getfly:
    Bước 2: Mình sử dụng thư viện RestClient để gọi API nên các bạn tải thư viện ở 2 trong 2 link dưới về.
    Bước 3: Tiếp theo bạn vào Function.php của themes điền đoạn code sau vào:
    PHP:
    add_action'woocommerce_thankyou''custom_woocommerce_auto_complete_order' );
    function 
    custom_woocommerce_auto_complete_order($order_id ) {
      if ( ! 
    $order_id ) {
        return;
      }
      
    $order wc_get_order$order_id );
      if(
    $order->get_status() == 'processing'//processing
      
    {
        
    $api = new RestClient([
            
    'base_url' => "https://username.getflycrm.com/api/v3/",
            
    'headers' => ['X-API-KEY' => 'key-api-ben-getflycrm''Content-Type' => 'application/json']
        ]);
        
    $arrproduct = array();
        foreach ( 
    $order->get_items() as $item_id => $item ) {
          
    $product $order->get_product_from_item$item );
          
    $sku $product->get_sku();
          
    array_push($arrproduct,array('product_code' => $sku,'product_name' => $item->get_name(),'quantity' => $item->get_quantity(),'price' =>  $item->get_subtotal(),'product_sale_off' => 'abc','cash_discount' => 'abc'));
        }
        
    $orderinfo = array('order_info' => array(
          
    'account_code' => $order->get_user_id(),
          
    'account_name' =>  $order->get_billing_first_name().' '.$order->get_billing_last_name(),
          
    'account_address' => $order->get_billing_address_1().', '.$order->get_billing_city().' Hoặc: '.$order->get_billing_address_2(),
          
    'account_email' => $order->get_billing_email(),
          
    'account_phone' =>  $order->get_billing_phone(),
          
    'order_date' =>  $order->get_date_created(),
          
    'discount' =>  $order->get_total_discount(),
          
    'discount_amount' =>  $order->get_total_discount(),
          
    'vat' =>  $order->get_total_tax(),
          
    'vat_amount' =>  $order->get_total_tax(),
          
    'transport' =>  $order->get_shipping_total(),
          
    'transport_amount' =>  $order->get_shipping_total(),
          
    'installation' =>  '',
          
    'installation_amount' =>  '',
          
    'amount' =>  $order->get_total()
          ),
          
    'products' => $arrproduct,
          
    'terms' => array('Thời gian giao hàng','Địa chỉ giao hàng','Điều khoản khác')
        );
        
    //Gửi ticket lên service
        
    $result $api->post("orders",$orderinfo);
        
    $order->update_status'completed' );
        echo 
    'API: Gửi đơn hàng thành công.';
        
    /*
        try
        {
          if($result->info->http_code == 200){
            //Service trả về dạng chuỗi json
              echo json_encode($result->decode_response());
          }
          else
          {
            echo $result->info->http_code.'-';
            echo json_encode($result->decode_response());
          }
        }
        catch(Exception $ex)
        {
            echo '{ "code" : "'. $ex->getMessage() .'"}';
        }
        */
      
    }else{echo 'Cảm ơn bạn đã xem lại đơn hàng';}
    }
    require(
    'includes/restclient.php'); //include thư viện gọi api
    • key-api-ben-getflycrm: là key bạn lấy được từ bên getfly.
    • username: tên usename của bạn bên getfly
    Như vậy là xong rồi, ra ngoài đặt hàng thử và kiểm tra nhé.

    Bạn có thể tham khảo các biến khác của đơn hàng tại link sau:
    Ngoài ra bạn có thể sự dụng 1 action khác như:
    PHP:
    add_action('woocommerce_checkout_order_processed''custom_woocommerce_auto_complete_order');
    function 
    custom_woocommerce_auto_complete_order($order_id ) {
    }
     
    Cảm ơn đã xem bài:

    Cách gọi sử dụng RESTful API Getfly lấy đơn hàng Woocommerce Wordpress

    Chỉnh sửa cuối: 21/4/21


Chủ để tương tự : Cách gọi
Diễn đàn Tiêu đề Date
Mã nguồn mở Wordpress Cách gọi sử dụng RESTful API Caresoft lấy đơn hàng Woocommerce Wordpress 20/4/21
Mã nguồn mở Wordpress Cách gọi sử dụng RESTful API Caresoft trong wordpress chi tiết 18/1/21
Mã nguồn mở Wordpress Menu mobile Flatsome lỗi Litespeed Cache không ấn được 24/1/24
Mã nguồn mở Wordpress Tối ưu Pluign LiteSpeed Cache tốc độ tốt cho website 5/1/24
Mã nguồn mở Wordpress Cách xử lý hack trên web wordpress như thế nào? 28/9/22