使用api将woocommerce结帐表单数据存储到自定义表

时间:2017-12-06 10:01:46

标签: wordpress

当客户点击下订单按钮时​​,我想在结账时将结账表单的值发送到我的API,以便我可以使用API​​存储这些数据。但我不知道如何使用钩子实现这一目标。请有人建议我这样做。

2 个答案:

答案 0 :(得分:0)

将下面的一个钩子放在你的function.php文件中,并在下订单后或在结账过程中检查过程。让你按照钩子的方式运行。

add_action(' woocommerce_checkout_order_processed',' my_status_pending',1,1);

add_action(' woocommerce_new_order',' my_status_pending',1,1);

或者,如果您想在付款后添加,请尝试以下代码:

add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);
function custom_process_order($order_id) {
    $order = new WC_Order( $order_id );
    $myuser_id = (int)$order->user_id;
    $user_info = get_userdata($myuser_id);
    $items = $order->get_items();
    foreach ($items as $item) {
        if ($item['product_id']==24) {
          // Do something clever
        }
    }
    return $order_id;
}

答案 1 :(得分:0)

你能尝试下面一次钩子吗? 当你点击进入购物车页面的结帐按钮时,这将有效。这将返回所有字段。在此,您可以传递您的API并传递您的详细信息,

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
    print_r($fields);   
    $url= 'https://restcountries.eu/rest/v1/name/United States'; //API URL
    $MSAPI_Call = curl_init();
    curl_setopt($MSAPI_Call, CURLOPT_URL,$url);
    curl_setopt($MSAPI_Call, CURLOPT_RETURNTRANSFER, 1);    
    curl_setopt($MSAPI_Call, CURLOPT_SSL_VERIFYPEER, 0);
    $response = curl_exec($MSAPI_Call);
    print_r($response);      
    die();    
     return $response;
}