'Hide a checkout custom required checkbox field based on chosen country in WooCommerce

I have the following scenario: If a customer is outside Canada, I need to show a checkbox that verifying he is out of the country for tax purposes, and to hide this checkbox and remove its requirement when the customer has selected Canada as a country. Looked for many solutions, I found this one useful and working, if I select Canada, the check box gets hidden, however the requirement still there, and the customer can't proceed further. Also, if he is a returning customer, and the system pulled Canada automatically, the checkbox shows unless he changes the country and goes back to Canada.

**What's missing from the code? **

  1. Remove checkbox requirement when Canada is selected
  2. Hide the checkbox on page load and only show after country selection if it was not Canada.

May you please help with this? (If other country is selected) (If Canada is selected)

add_action( 'woocommerce_review_order_before_submit', 'bt_add_tax_verification', 10 );
/**
 * Add WooCommerce additional Checkbox checkout field
 */
function bt_add_tax_verification() {
   
    woocommerce_form_field( 'tax_verification', array( // CSS ID
       'type'          => 'checkbox',
       'class'         => array('form-row mycheckbox'), // CSS Class
       'label_class'   => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
       'input_class'   => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'),
       'required'      => true, // Mandatory or Optional
       'label'         => 'I certify that I am not resident in Canada for purposes of the Excise Tax Act and I am not registered under that Act. Where applicable, I agree to advise RxCourse Institute Inc, 838 Silverthorn Mill Ave, Mississauga ON L5W 1B1 in the event there is any change to my residence status or should I become registered for the purposes of the Excise Tax Act', // Label and Link
   ));    
}

add_action( 'woocommerce_checkout_process', 'bt_add_tax_verification_warning' );
/**
 * Alert if checkbox not checked
 */ 
function bt_add_tax_verification_warning() {
    if ( ! (int) isset( $_POST['tax_verification'] ) ) {
        wc_add_notice( __( 'Please verify that you are not a Canadian resident by selecting the checkbox' ), 'error' );
    }
}
add_action( 'woocommerce_checkout_update_order_meta', 'bt_checkout_field_order_meta_db' );
/**
 * Add custom field as order meta with field value to database
 */
function bt_checkout_field_order_meta_db( $order_id ) {
    if ( ! empty( $_POST['tax_verification'] ) ) {
        update_post_meta( $order_id, 'tax_verification', sanitize_text_field( $_POST['tax_verification'] ) );
    }
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'bt_checkout_field_display_admin_order_meta', 10, 1 );
/**
 * Display field value on the backend WooCOmmerce order
 */
function bt_checkout_field_display_admin_order_meta($order){
    echo '<p><strong>'.__('Checkout Checkbox Label').':</strong> ' . get_post_meta( $order->get_id(), 'tax_verification', true ) . '<p>'; 
}
add_filter('woocommerce_email_order_meta_keys', 'bt_custom_order_meta_email');

function bt_custom_order_meta_email( $keys ) {
     $keys[] = 'tax_verification'; // This will look for a custom field called 'tax_verification' and add it to WooCommerce emails
     return $keys;
}
add_filter( 'woocommerce_email_order_meta_fields', 'bt_woocommerce_email_order_meta_fields', 10, 3 );
/**
 * Show hide base on country selection
 */

add_action( 'woocommerce_after_checkout_form', 'woo_conditionally_hide_show_checkout_field', 9999 );

function woo_conditionally_hide_show_checkout_field() {
wc_enqueue_js( "
    // On page load
    jQuery(document).ready(function() {
        // var country_code = jQuery(this).val();
        var country_name = jQuery('#billing_country option:selected').text().toLowerCase();
        show_hide_fields(country_name);
    });

    // On change country dropdown
    jQuery(document).on('change','#billing_country',function() {
        var country_name = jQuery('option:selected',this).text().toLowerCase();
        show_hide_fields(country_name);
    });

    function show_hide_fields(country){
        if (country == 'canada') {
            jQuery('#tax_verification_field').hide();
        } else {
            jQuery('#tax_verification_field').show();
            
        }
    }
");
}


Solution 1:[1]

add_action( 'woocommerce_checkout_process', 'bt_add_tax_verification_warning' );
/**
 * Alert if checkbox not checked
 */ 
function bt_add_tax_verification_warning() {

    if( isset($_POST['billing_country']) && 'CA' !== $_POST['billing_country'] ){
        if ( ! (int) isset( $_POST['tax_verification'] ) ) {
            wc_add_notice( __( 'Please verify that you are not a Canadian resident by selecting the checkbox' ), 'error' );
        }
    }
}

Modify the validation function to check if Country is CA

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mujuonly