在Woocommerce中捕获自定义结帐字段值 [英] Capture custom checkout field value in Woocommerce

查看:70
本文介绍了在Woocommerce中捕获自定义结帐字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在woocommerce上,正在尝试通过代码片段尝试获取所有字段的值,以便使用json发送到adroid应用。

Im on woocommerce and are trying by snipets, get the value of all fields to send to adroid app with json.

麻烦之处在于,我无法捕获结帐时自定义字段的值。我尝试通过插件和代码段创建字段,但是我无法恢复要发送到json的值

The trouble its, that i cant capture the values of custom fields on check out. I tried by plugin and snippet create the field but, i cant recover the values to send to json

该字段是使用该代码段

add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );

function my_custom_checkout_field( $checkout ) {

echo '<div id="my_custom_checkout_field"><h2>' . __('My Field') . '</h2>';

woocommerce_form_field( 'my_field_name', array(
    'type'          => 'text',
    'class'         => array('my-field-class form-row-wide'),
    'label'         => __('Fill in this field'),
    'placeholder'   => __('Enter something'),
    ), $checkout->get_value( 'my_field_name' ));

echo '</div>';

}

当尝试使用该片段创建json时,我可以获得所有标准woo值,但我无法获取自定义字段

when try to create the json with that snipet i can get all standard woo values but i cant get the custom field

function action_woocommerce_order_status_completed( $array ) { 

$order = wc_get_order( $order_id );
$url = 'localhost/wp/recolector.php';

//Initiate cURL.
$ch = curl_init($url);

// now you can get the data from $product array
};

 //The JSON data.
$jsonData = array(
'email' => $order->get_billing_email(),
'first_name'=> $order->get_billing_first_name(),
'last_name' => $order->get_billing_last_name(),
'phone'=>$order->get_billing_phone()(),
'address'=>$order->get_billing_address_1(),
'city'=>$order->get_billing_city(),
'province'=>$order->get_billing_state(),
'country'=>$order->get_billing_country(),
'order'=>$order->get_order_number()
);

//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);

//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);

//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 

//Execute the request
$result = curl_exec($ch);


};

// add the action
add_action( 'woocommerce_order_status_completed', 'action_woocommerce_order_status_completed', 10, 1 ); 

那么如何将my_field_name添加到json中呢?

well how supose can add my_field_name to json?

感谢帮助。

推荐答案

已更新

代码中有一些错误和遗漏。请尝试以下操作:

There are some mistakes and missing things in your code. Try the following instead:

// Display a custom checkout field
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
    echo '<div id="my_custom_checkout_field"><h2>' . __('My Field') . '</h2>';

    woocommerce_form_field( '_custom_field', array(
        'type'          => 'text',
        'class'         => array('my-custom-field form-row-wide'),
        'label'         => __('Fill in this field'),
        'placeholder'   => __('Enter something'),
        ), $checkout->get_value( '_custom_field' ));

    echo '</div>';
}

// Save the custom checkout field in the order meta
add_action( 'woocommerce_checkout_create_order', 'save_order_custom_meta_data', 10, 2 );
function save_order_custom_meta_data( $order, $data ) {
    if ( isset($_POST['_custom_field']) )
        $order->update_meta_data('_custom_field', sanitize_text_field( $_POST['_custom_field'] ) );
}


add_action( 'woocommerce_order_status_completed', 'action_on_order_status_completed', 10, 2 ); 
function action_on_order_status_completed( $order_id, $order ) { 
    // The order data (array)
    $data = array(
        'email'         => $order->get_billing_email(),
        'first_name'    => $order->get_billing_first_name(),
        'last_name'     => $order->get_billing_last_name(),
        'phone'         => $order->get_billing_phone()(),
        'address'       => $order->get_billing_address_1(),
        'city'          => $order->get_billing_city(),
        'province'      => $order->get_billing_state(),
        'country'       => $order->get_billing_country(),
        'order'         => $order->get_order_number()
    );

    // Get the custom field value (and add it if not empty)
    if( $custom_field = $order->get_meta('_custom_field') ) {
        // Add it to the array
        $data['custom_field'] = $custom_field;
    }

    //Encode the array into JSON.
    $json_data = json_encode( $data ); 

    // The Url
    $url = 'localhost/wp/recolector.php';

    //Initiate cURL.
    $ch = curl_init($url);

    //Tell cURL that we want to send a POST request.
    curl_setopt($ch, CURLOPT_POST, 1);

    //Attach our encoded JSON string to the POST fields.
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

    //Set the content type to application/json
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 

    //Execute the request
    $result = curl_exec($ch);

    // Set the return result in the order as custom meta data (if needed)
    $order->update_meta_data('_curl_result', $result );
    $order->save();
}

现在应该更好地工作了。

It should better work now.

这篇关于在Woocommerce中捕获自定义结帐字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆