2
votes

I have an ecommerce webiste using woocommerce In the checkout page I need to activate a custom required field "Codice Fiscale" if the billing country is set to "Italy", otherwise that extra field must be removed The code in my child theme functions.php is

add_filter( 'woocommerce_checkout_fields' , 'field_cfpiva1' );

function field_cfpiva1( $fields ) {
 $fields['billing']['billing_cf'] = array(
  'label'     => __('Codice Fiscale', 'woocommerce'),
  'placeholder'   => _x('Codice Fiscale', 'placeholder', 'woocommerce'),
  'required'  => false,
  'class'     => array('form-row-wide'),
  'clear'     => true
 );

 return $fields;
}

add_filter( 'woocommerce_admin_billing_fields' , 'admin_field_cfpiva1' );

function admin_field_cfpiva1( $fields ) {
 $fields['cf'] = array(
  'label' => __('Codice Fiscale', 'woocommerce'),
  'show'  => true
 );
 return $fields;
}

But I've no idea on how to do this dynamically on country change

2

2 Answers

2
votes

I know this question is a bit old, but here was my solution to change the maxlength of the zip code field. My client was using the WooCommerce Table Rates shipping plugin, and in the USA, if the zip code entered contained the full 9 digits (xxxxx-xxxx), the plugin wouldn't properly calculate shipping. We were charging international rates for people in the same state.

I was going to use a hook to limit the post_code field to 5, but many countries have longer post code strings (like Canada, which is 6). Thanks to #Sonic Advisor. I was able to quickly modify the code to selectively change the maxlength attribute of the post_code form field as shown below:

<script>
//Limit zip code to 5 digits for United States ONLY
    jQuery(document).ready(function (){

         if (jQuery('#billing_country').val() == 'US'){
            jQuery('#billing_postcode').attr('maxlength','5');

        }

        jQuery('#billing_country').on('change',function() {
                if (jQuery('#billing_country').val() == 'US'){
                jQuery('#billing_postcode').attr('maxlength','5');  
            } else {
            jQuery('#billing_postcode').attr('maxlength','15');

            }
        })

        if (jQuery('#shipping_country').val() == 'US'){
            jQuery('#shipping_postcode').attr('maxlength','5');

        }

        jQuery('#shipping_country').on('change',function() {
                if (jQuery('#shipping_country').val() == 'US'){
                jQuery('#shipping_postcode').attr('maxlength','5');  
            } else {
            jQuery('#shipping_postcode').attr('maxlength','15');

            }
        })
    })
    </script>
-1
votes

I've been attempting to achieve something very similar, but instead to show a custom field when a particular shipping method is selected.

Previously I had the following jquery successfully working by adding it to the cart-shipping.php template, but I can't seem to get it working on the 'state' field. Perhaps this may help (both of us) somehow reach the answer we're both after...

<script>
    $(document).ready(function (){

        if ($('#shipping_method_0').val() == 'flat_rate:delivered-vic-only'){
            $('#newfield').show();
        }

        $('#shipping_method_0').on('change',function() {
                if ($('#shipping_method_0').val() == 'flat_rate:delivered-vic-only'){
                $('#newfield').show();  
            } else {
            $('#newfield').hide();

            }
        })
    })
    </script>