2017-08-03 112 views
1

當管理員在收到貨到付款時將訂單狀態從掛起更改爲完成時,我想發送API請求。在這種情況下,我無法使用謝謝你的頁面鉤子。實際上,當用戶使用WordPress網站購買課程時,我想使用他們的API將他加入到第三方LMS「Canvas」課程中。 這是代碼:訂單付款狀態變更時的woocommerce掛接

// enroll student on buying course 
add_action('woocommerce_order_status_completed', 'enroll_student', 10,1); 


function enroll_student($order_id ) { 


$order = new WC_Order($order_id); 
$user = get_current_user_id(); 
global $wpdb; 
$canvas_user = $wpdb->get_var('SELECT canvas_id FROM wp_user_map where wp_id ='.$user); 
foreach ($order->get_items() as $item) { 

    if($item['variation_id'] > 0){ 
     $product_id = $item['variation_id']; // variable product 
    } else { 
     $product_id = $item['product_id']; // simple product 
    } 

    // Get the product object 
    $product = wc_get_product($product_id); 
} 
$prod_id = $product->id; 
global $wpdb; 
$canvas_prod_id = $wpdb->get_var('SELECT canvas_id FROM wp_course_map where wp_id ='.$prod_id); 
$url = "https://myurl/api/v1/courses/".$canvas_prod_id."/enrollments"; 
$token = 'mytoken'; 
$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, $url); 
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' .$token)); 
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); 

curl_setopt($curl, CURLOPT_POSTFIELDS, array(
    'enrollment[user_id]'=>$canvas_user, 
    'enrollment[type]'=>'StudentEnrollment', 

)); 
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
curl_setopt($curl, CURLOPT_ENCODING, ""); 
$curlData = curl_exec($curl); 
//echo $curlData; 
//echo $name; 
//echo $email; 
//echo 'cur user:'.$user; 
//echo 'cavas user id:'.$canvas_user; 
//echo 'Prod id:'.$prod_id; 
//echo 'canvas Prod id:'.$canvas_prod_id; 

curl_close($curl); 
} 

此代碼的工作完美,如果我使用三江源頁面掛鉤,而不是與此掛鉤:woocommerce_payment_complete_order_status或任何其他付款 可以將某些PLZ點我這鉤子可以幫助我做到這一點?

+0

你試過'add_action('woocommerce_order_status_changed','your_function',99,3); 功能your_function($ ORDER_ID,$ OLD_STATUS,$ NEW_STATUS){ 如果($ NEW_STATUS == 「完成」){// 您的代碼在這裏 } }'? – vel

+0

@vel試過這個,不起作用 –

+0

請檢查這個'https:// stackoverflow.com/questions/36315825/woocommerce-order-status-completed-not-triggered' – vel

回答

1

我的邏輯有一個錯誤,我得到當前登錄用戶的用戶標識,但我需要訂購產品的用戶的用戶標識。解決了使用訂單ID對象

相關問題