skip to Main Content

I am not a developer and I don’t know much about code so I’m struggling a bit with this.
I have a shop that ships mainly to Peru, but i want to offer shipping to other countries now. The problem I have is that there are more fields for shipping within Peru while for other countries those are not necessary so I would like to hide/remove those when any other country that isn’t Peru is selected.

I tried using the code below while setting a flat rate (flat_rate:3) for all other countries. The following three fields would hide ‘billing_id’, ‘billing_address_2’ , ‘billing_reference’. When changing countries automatically the shipping option would change to flat rate and the fields would hide, it was working yesterday but today it only works if i change the country and then refresh the site. I’m not sure if i’m doing something wrong, or if there’s any other way I could do it. I am using Code Snippets to add the code.

/* This piece of code will hide fields for the chosen method.
.hide_pickup {
    display: none !important;
}
*/
 
// Hide Local Pickup shipping method
add_filter( 'woocommerce_checkout_fields', 'hide_local_pickup_method' );
function hide_local_pickup_method( $fields_pickup ) {
    // change below for the method
    $shipping_method_pickup ='flat_rate:3';
    // change below for the list of fields. Add (or delete) the field name you want (or don’t want) to use
    $hide_fields_pickup = array( 'billing_id', 'billing_address_2' , 'billing_reference' );
 
    $chosen_methods_pickup = WC()->session->get( 'chosen_shipping_methods' );
    $chosen_shipping_pickup = $chosen_methods_pickup[0];
 
    foreach($hide_fields_pickup as $field_pickup ) {
        if ($chosen_shipping_pickup == $shipping_method_pickup) {
            $fields_pickup['billing'][$field_pickup]['required'] = false;
            $fields_pickup['billing'][$field_pickup]['class'][] = 'hide_pickup';
        }
        $fields_pickup['billing'][$field_pickup]['class'][] = 'billing-dynamic_pickup';
    }
    return $fields_pickup;
}
// Local Pickup - hide fields
add_action( 'wp_head', 'local_pickup_fields', 999 );
function local_pickup_fields() {
    if (is_checkout()) :
    ?>
    <style>
        .hide_pickup {display: none!important;}
    </style>
    <script>
        jQuery( function( $ ) {
            if ( typeof woocommerce_params === 'undefined' ) {
                return false;
            }
            $(document).on( 'change', '#shipping_method input[type="radio"]', function() {
                // change flat_rate:3 accordingly
            $('.billing-dynamic_pickup').toggleClass('hide_pickup', this.value == 'flat_rate:3');
            });
        });
    </script>
    <?php
    endif;
}

2

Answers


  1. Chosen as BEST ANSWER

    Thanks to Bhautik who told me to use updated_checkout, I am leaving here the code i am using and that works correctly for me!

    add_filter('woocommerce_checkout_fields', 'remove_peru_billing_checkout_fields');
    
    function remove_peru_billing_checkout_fields($fields) {
    $shipping_method ='flat_rate:3';
    // 'flat_rate:3' shipping method can be changed.
    global $woocommerce;
    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    $chosen_shipping = $chosen_methods[0];
    
    if ($chosen_shipping == $shipping_method) {
    unset($fields['billing']['billing_id']); // IDs to hide
    unset($fields['billing']['billing_address_2']);
    unset($fields['billing']['billing_reference']);
    }
    return $fields;
    }
    
    add_action('wp_footer', 'billing_country_update_checkout', 50);
    function billing_country_update_checkout() {
        if ( ! is_checkout() ) return;
        ?>
       <script type="text/javascript">
        let isRefreshPage = false;
        jQuery('form.checkout').on('change', 'select[name=billing_country]' ,function() {
            var val = jQuery( this ).val();
            if (val) {
                isRefreshPage = true;
            }
        });
         
        jQuery('body').on('updated_checkout', function(){
            if (isRefreshPage) {
                location.reload();
            }
        });
    </script>
        <?php
    }


  2. You can use the updated_checkout trigger that is called after the checkout update. try the below code.

    // Hide Local Pickup shipping method
    add_filter( 'woocommerce_checkout_fields', 'hide_local_pickup_method' );
    function hide_local_pickup_method( $fields_pickup ) {
        // change below for the method
        $shipping_method_pickup ='flat_rate:3';
        // change below for the list of fields. Add (or delete) the field name you want (or don’t want) to use
        $hide_fields_pickup = array( 'billing_id', 'billing_address_2' , 'billing_reference' );
     
        $chosen_methods_pickup = WC()->session->get( 'chosen_shipping_methods' );
        $chosen_shipping_pickup = $chosen_methods_pickup[0];
     
        foreach($hide_fields_pickup as $field_pickup ) {
            if ($chosen_shipping_pickup == $shipping_method_pickup) {
                $fields_pickup['billing'][$field_pickup]['required'] = false;
                $fields_pickup['billing'][$field_pickup]['class'][] = 'hide_pickup';
            }
            $fields_pickup['billing'][$field_pickup]['class'][] = 'billing-dynamic_pickup';
        }
        return $fields_pickup;
    }
    // Local Pickup - hide fields
    add_action( 'wp_head', 'local_pickup_fields', 999 );
    function local_pickup_fields() {
        if (is_checkout()) :
        ?>
        <style>
            .hide_pickup {display: none!important;}
        </style>
        <script>
            jQuery( function( $ ) {
                if ( typeof woocommerce_params === 'undefined' ) {
                    return false;
                }
    
                $(document).on( 'change', '#shipping_method input[type="radio"]', function() {
                    // change flat_rate:3 accordingly
                    $('.billing-dynamic_pickup').toggleClass('hide_pickup', this.value == 'flat_rate:3');
                });
    
                $( document ).on('updated_checkout', function(event) {  
                    // change flat_rate:3 accordingly
                    $('.billing-dynamic_pickup').toggleClass('hide_pickup', $('.shipping_method:checked').val() == 'flat_rate:3');
                });
    
            });
        </script>
        <?php
        endif;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search