我想在管理员在收到货到付款时将订单状态从挂起更改为已完成时发送API请求。在这种情况下,我无法使用感谢页面钩子。实际上,当用户使用WordPress网站购买课程时,我想让他参加第三方LMS" Canvas"使用他们的API。 这是代码:
// 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);
}
如果我使用thankyou页面挂钩,此代码可以正常工作,但不能使用此挂钩:woocommerce_payment_complete_order_status或任何其他付款 有些人可以指出我哪个钩可以帮我实现这个目标吗?
答案 0 :(得分:1)
我的逻辑中出现错误,我获得了当前登录用户的用户ID,但我需要订购该产品的用户的用户ID。使用订单ID对象
解决了这个问题