skip to Main Content

I already add checkbox to show/hide custom field in my WooCommerce Checkout page at functions.php and the code is working well but I can’t get input data from my customer. Where/How can I get it? I’ve try so many code and losting my time too much.

Here is my code. Please help 🙁

add_filter( 'woocommerce_checkout_fields' , 'rws_display_checkbox_and_new_checkout_field' ); 
function rws_display_checkbox_and_new_checkout_field( $fields ) {
$fields['billing']['have_vat_number'] = array(
    'type'      => 'checkbox',
    'label'     => __('ต้องการใบกำกับภาษี', 'woocommerce'),
    'class'     => array('form-row-wide'),
    'clear'     => true
);   
$fields['billing']['vat_name'] = array(
    'label'     => __('ชื่อ – สกุล / บริษัท', 'woocommerce'),
    'placeholder'   => _x(' ', 'placeholder', 'woocommerce'),
    'class'     => array('form-row-wide'),
    'clear'     => true
);
$fields['billing']['vat_number'] = array(
    'label'     => __('หมายเลขผู้เสียภาษี', 'woocommerce'),
    'placeholder'   => _x(' ', 'placeholder', 'woocommerce'),
    'class'     => array('form-row-wide'),
    'clear'     => true
);
$fields['billing']['vat_address'] = array(
    'type'      => 'textarea',
    'label'     => __('ที่อยู่', 'woocommerce'),
    'placeholder'   => _x(' ', 'placeholder', 'woocommerce'),
    'class'     => array('form-row-wide'),
    'clear'     => true
);
return $fields;
}
add_action( 'woocommerce_after_checkout_form', 'rws_conditionally_hide_show_new_field', 9999 );
function rws_conditionally_hide_show_new_field() {
  wc_enqueue_js( "
      jQuery('input#have_vat_number').change(function(){
         if (! this.checked) {
            jQuery('#vat_name_field').fadeOut();
            jQuery('#vat_name_field input').val('');
            jQuery('#vat_number_field').fadeOut();
            jQuery('#vat_number_field input').val(''); 
            jQuery('#vat_address_field').fadeOut();
            jQuery('#vat_address_field input').val(''); 
         } else {
            jQuery('#vat_name_field').fadeIn();
            jQuery('#vat_number_field').fadeIn();
            jQuery('#vat_address_field').fadeIn();
         } 
      }).change();
  ");  
    if ( isset( $_POST['billing']['have_vat_number'] ) ) {
        $have_vat_number = $_POST['billing']['have_vat_number'];
    }

    if ( isset( $_POST['billing']['vat_name'] ) ) {
        $vat_name = $_POST['billing']['vat_name'];
    }

    if ( isset( $_POST['billing']['vat_number'] ) ) {
        $vat_number = $_POST['billing']['vat_number'];
    }

    if ( isset( $_POST['billing']['vat_address'] ) ) {
        $vat_address = $_POST['billing']['vat_address'];
    }
}

I’ve try by add this code but still not working.

add_action('woocommerce_checkout_update_order_meta', 'rws_save_custom_checkout_field_data');

function rws_save_custom_checkout_field_data($order_id) {
    if ($_POST['billing']['have_vat_number']) {
        $have_vat_number = sanitize_text_field($_POST['billing']['have_vat_number']);
        update_post_meta($order_id, 'Have VAT Number', $have_vat_number);
    }

    if ($_POST['billing']['vat_name']) {
        $vat_name = sanitize_text_field($_POST['billing']['vat_name']);
        update_post_meta($order_id, 'VAT Name', $vat_name);
    }

    if ($_POST['billing']['vat_number']) {
        $vat_number = sanitize_text_field($_POST['billing']['vat_number']);
        update_post_meta($order_id, 'VAT Number', $vat_number);
    }

    if ($_POST['billing']['vat_address']) {
        $vat_address = sanitize_textarea_field($_POST['billing']['vat_address']);
        update_post_meta($order_id, 'VAT Address', $vat_address);
    }
}

2

Answers


  1. Chosen as BEST ANSWER

    now I've try with the new way by add custom field with wordpress plugin then add Javascripts code in functions.php and it hide only label box the name of label doesn't hide.

    My code

    Results


  2. You can always use a trick – var_dump($_POST);exit; or error_log(print_r($_POST,true));
    With this you will get to know how it’s send on form submission.

    Try $_POST[‘vat_name’] instead of $_POST[‘billing’][‘vat_name’]

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search