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

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

  1. admin
    Tham gia ngày:
    22/5/13
    Bài viết:
    4,883
    Đã được thích:
    1,193
    Điểm thành tích:
    113
    Giới tính:
    Nam
    Bài viết hướng dẫn cách sử dụng Restfull API để lấy đơn hàng khi hoàn thành đơn hàng sử dụng plugin Wooceommerce cho vào phần mềm Caresoft.vn mới nhất đảm bảo 100% làm được với mọi người dù biết code hay không.

    Demo:

    Hiển thị tại danh sách: Phiếu ghi từ kênh WEB

    phieu-ghi-tu-kenh-web-api-caresoft-woocommerace.jpg

    Chi tiết đơn hàng:

    api-dat-hang-woocommerace.jpg

    Code chỉ cần copy bỏ vào không cần chỉnh sửa gì nhiều.

    Các bước thực hiện

    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:
    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: Chèn code sau vào function.php của themes:
    PHP:
    //add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
    add_action('woocommerce_checkout_order_processed''custom_woocommerce_auto_complete_order');
    function 
    custom_woocommerce_auto_complete_order($order_id ) {
        if ( ! 
    $order_id ) {
            return;
        }
        try{
            
    $api = new RestClient([
                
    'base_url' => "https://api.caresoft.vn/NewTechPharm/api/v1/",
                
    'headers' => ['Authorization' => 'Bearer 1111111111LrPw''Content-Type' => 'application/json']
            ]);
        }
        catch(
    Exception $ex)
        {
            
    //echo '{ "code" : "'. $ex->getMessage() .'"}';
            
    return;
        }

        
    $order wc_get_order$order_id );
        
    $cus_comment .= 'Thông tin sản phẩm bao gồm: ';
        
    $cus_comment .= 'Địa chỉ:  '.$order->get_billing_address_1().', '.$order->get_billing_city().' '.$order->get_billing_address_2();
        
    $arrproduct = array();
        foreach ( 
    $order->get_items() as $item_id => $item ) {
            
    $product $order->get_product_from_item$item );
            
    $sku $product->get_sku();
            
    $cus_comment .= 'Tên: '.$item->get_name().', Sơ lượng: '.$item->get_quantity().', tiền: '.$item->get_subtotal().' ||| ';
            
    array_push($arrproduct,array('Id' => 1,'value' => 'Tên: '.$item->get_name().', Sơ lượng: '.$item->get_quantity().', tiền: '.$item->get_subtotal()));
        }
     
        
    //Truyền tham số
     
        
    $cus_name $order->get_billing_first_name().' '.$order->get_billing_last_name();
        
    $cus_email $order->get_billing_email();
        
    $cus_phone $order->get_billing_phone();

        
    $tickets = array('ticket' => array(
                
    'ticket_subject' => 'Đặt hàng từ Nacurgo Gel (nacurgogel.com)-(mới)',
                
    'ticket_comment' =>  $cus_comment,
                
    'ticket_source' => 'Web',
                
    //'assignee_id' => 6878985, //ID của Thúy Hằng
                
    'group_id' => 6792,
                
    'email' =>  $cus_email,
                
    'phone' =>  $cus_phone,
                
    'username' =>  $cus_name
                
    //,"custom_fields" => $arrproduct
            
    ));
        
    //echo 'API: Gửi đơn hàng thành công.';
        //Gửi ticket lên service
        
    try
        {
            
    $result $api->post("tickets",$tickets);
            
    //echo 'Mã phản hồi: '.$result->info->http_code;

        
    }
        catch(
    Exception $ex)
        {
            
    //echo '{ "code" : "'. $ex->getMessage() .'"}';
            
    return;
        }
      }
    require(
    'includes/restclient.php'); //include thư viện gọi api

    • username: chính là tên đăng nhập domain trên caresoft
    • key-api: API của bạn trên caresoft (lấy tại: quản trị admin -> Dịch Vụ -> API -> Api token (tạo mới nếu chưa có)
    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:
    .Để lấy truyền dữ liệu từ các form bất kỳ như Contact Form 7,... đọc bài sau: https://itseovn.com/threads/cach-goi-su-dung-restful-api-caresoft-trong-wordpress-chi-tiet.321963/
     
    Cảm ơn đã xem bài:

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

    Chỉnh sửa cuối: 5/9/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 Getfly lấy đơn hàng Woocommerce Wordpress 3/3/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