添加隐藏字段以结帐并通过订单处理

Adding a hidden fields to checkout and process it through order

我想在结帐过程中添加一个验证码,该验证码是只读的(或不可见的)和预填写的,固定在订单上。 客户需要此代码来验证订单。

我将自定义数组添加到 woocommerce_checkout_fields 过滤器的帐单字段:

//VID
$fields['billing']['billing_vid'] = array( 
'label'     => __('', 'woocommerce'), 
'placeholder'   => _x('', 'placeholder', 'woocommerce'), 
'required'  => false,
'type'      => 'text', 
'class'     => array('form-row-wide'), 
'clear'     => false,
'default' => wp_rand(10000,99999)   
);

这有效,但客户仍然可以在结帐过程中在该字段中写入内容。

有人能帮我解决吗?

谢谢

更新 2增加了对 Woocommerce 3+ 的兼容性并删除了一些错误

正如您所期望的,这里可以提供更完整的解决方案:

// Outputting the hidden field in checkout page
add_action( 'woocommerce_after_order_notes', 'add_custom_checkout_hidden_field' );
function add_custom_checkout_hidden_field( $checkout ) {

    // Generating the VID number
    $vid_number = wp_rand(10000,99999);

    // Output the hidden field
    echo '<div id="user_link_hidden_checkout_field">
            <input type="hidden" class="input-hidden" name="billing_vid" id="billing_vid" value="' . $vid_number . '">
    </div>';
}

// Saving the hidden field value in the order metadata
add_action( 'woocommerce_checkout_update_order_meta', 'save_custom_checkout_hidden_field' );
function save_custom_checkout_hidden_field( $order_id ) {
    if ( ! empty( $_POST['billing_vid'] ) ) {
        update_post_meta( $order_id, '_billing_vid', sanitize_text_field( $_POST['billing_vid'] ) );
    }
}

// Displaying "Verification ID" in customer order
add_action( 'woocommerce_order_details_after_customer_details', 'display_verification_id_in_customer_order', 10 );
function display_verification_id_in_customer_order( $order ) {
    // compatibility with WC +3
    $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;

    echo '<p class="verification-id"><strong>'.__('Verification ID', 'woocommerce') . ':</strong> ' . get_post_meta( $order_id, '_billing_vid', true ) .'</p>';
}

 // Display "Verification ID" on Admin order edit page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_verification_id_in_admin_order_meta', 10, 1 );
function display_verification_id_in_admin_order_meta( $order ) {
    // compatibility with WC +3
    $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
    echo '<p><strong>'.__('Verification ID', 'woocommerce').':</strong> ' . get_post_meta( $order_id, '_billing_vid', true ) . '</p>';
}

// Displaying "Verification ID" on email notifications
add_action('woocommerce_email_customer_details','add_verification_id_to_emails_notifications', 15, 4 );
function add_verification_id_to_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {
    // compatibility with WC +3
    $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;

    $output = '';
    $billing_vid = get_post_meta( $order_id, '_billing_vid', true );

    if ( !empty($billing_vid) )
        $output .= '<div><strong>' . __( "Verification ID:", "woocommerce" ) . '</strong> <span class="text">' . $billing_vid . '</span></div>';

    echo $output;
}

代码在您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

此代码已经过测试并且有效。