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. 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: 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: https://developer.getfly.vn/#n-h-ng 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ề. https://github.com/tcdent/php-restclient Hoặc: https://itseovn.com/i/file/restclient.zip Tải về xong, giải nén ra, copy file restclient.php cho vào themes của web: wp-content/themes/Tên-Themes/includes/restclient.zip. 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: https://www.businessbloomer.com/woocommerce-easily-get-order-info-total-items-etc-from-order-object/