4

I'm trying to hide checkout fields based on shipping method.

function premove_billing_checkout_fields($fields) {
    global $woocommerce;
    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    $chosen_shipping = $chosen_methods[0];

    if( $chosen_shipping === 'local_pickup:20' ) {
       unset($fields['billing']['billing_postcode']);
       unset($fields['billing']['billing_state']);
       unset($fields['billing']['billing_country']);
    }

    if( $chosen_shipping === 'wc_custom_shipping_pickpoint' ) {
       unset($fields['billing']['billing_postcode']);
       unset($fields['billing']['billing_address_1']);
       unset($fields['billing']['billing_state']);
    }
    return $fields;
}
add_filter('woocommerce_checkout_fields', 
'premove_billing_checkout_fields', 990 );

This code is working, but to hide the fields I need to refresh the page. How can I hide the fields using Ajax?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Nikolay Fomin
  • 43
  • 1
  • 5
  • I tried to find the answer to my question in the search, but it was not specifically found. PickPoint is one of the delivery services. If you manage to hide the fields for local_pickup, then for pickpoint I can do by analogy. Through the ID I do not work hiding the fields in general. ID local_pickup:20 is "shipping_method_0_local_pickup20" ID wc_custom_shipping_pickpoint is "shipping_method_0_wc_custom_shipping_pickpoint" – Nikolay Fomin Oct 09 '17 at 08:10
  • This is a question about that problem, but nobody answered. https://stackoverflow.com/questions/46195765/hide-woocommerce-checkout-fields-by-shipping-type-selected-on-checkout-page?rq=1 – Nikolay Fomin Oct 09 '17 at 08:16

1 Answers1

11

Update 4 (made on June 2018)
- Added a delay on initialization specifically for Woocommerce 3.4.x.
- Solved a bug when shipping address checkbox is enabled.
- Enhanced lighter and more effective jQuery code.
- Added conditional fields validation

You don't need any Ajax to achieve this. The first function will make all necessary checkout fields not "required", as this is necessary to be able to conditionally show / hide checkout fields. The second function (mostly jQuery), will show / hide your desired fields depending on the chosen shipping method.

// Conditional Show hide checkout fields based on chosen shipping methods
add_action( 'wp_footer', 'custom_checkout_field_script' );
function custom_checkout_field_script() {

    // HERE your shipping methods rate IDs
    $local_pickup = 'local_pickup:20';
    $pickpoint = 'wc_custom_shipping_pickpoint';

    $required_text = esc_attr__( 'required', 'woocommerce' );
    $required_html = '<abbr class="required" title="' . $required_text . '">*</abbr>';
    ?>
    <script>
        jQuery(function($){
            var ism = 'input[name^="shipping_method"]',         ismc = ism+':checked',
                csa = 'input#ship-to-different-address-checkbox',
                rq = '-required',       vr = 'validate'+rq,     w = 'woocommerce',      wv = w+'-validated',
                iv = '-invalid',        fi = '-field',          wir = w+iv+' '+w+iv+rq+fi,
                b = '#billing_',        s = '#shipping_',       f = '_field',
                a1 = 'country',     a2 = 'address_1',   a3 = 'address_2',   a4 = 'postcode',    a5 = 'state',
                b1 = b+a1+f,        b2 = b+a2+f,        b3 = b+a3+f,        b4 = b+a4+f,        b5 = b+a5+f,
                s1 = s+a1+f,        s2 = s+a2+f,        s3 = s+a3+f,        s4 = s+a4+f,        s5 = s+a5+f,
                pickPoint = '<?php echo $pickpoint; ?>',        localPickup = '<?php echo $local_pickup; ?>';

            // Utility function to shows or hide checkout fields
            function showHide( action='show', selector='' ){
                if( action == 'show' )
                    $(selector).show(function(){
                        $(this).addClass(vr);
                        $(this).removeClass(wv);
                        $(this).removeClass(wir);
                        if( $(selector+' > label > abbr').html() == undefined )
                            $(selector+' label').append('<?php echo $required_html; ?>');
                    });
                else
                    $(selector).hide(function(){
                        $(this).removeClass(vr);
                        $(this).removeClass(wv);
                        $(this).removeClass(wir);
                        if( $(selector+' > label > abbr').html() != undefined )
                            $(selector+' label > .required').remove();
                    });
            }

            // Initializing at start after checkout init (Based on the chosen shipping method)
            setTimeout(function(){
                if( $(ismc).val() == pickPoint ) // Chosen "Pick point" (Hiding "Delivery")
                {
                    showHide('show',b1 ); // Country
                    showHide('hide',b2 ); // Address 1
                    showHide('hide',b3 ); // Address 2
                    showHide('hide',b4 ); // Postcode
                    showHide('hide',b5 ); // State
                }
                else if( $(ismc).val() == localPickup ) // Choosen "Local pickup" (Hidding "Take away")
                {
                    showHide('hide',b1);
                    showHide('show',b2);
                    showHide('show',b3);
                    showHide('hide',b4);
                    showHide('hide',b5);
                }
                else
                {
                    showHide('show',b1);
                    showHide('show',b2);
                    showHide('show',b3);
                    showHide('show',b4);
                    showHide('show',b5);
                }
            }, 100);

            // When shipping method is changed (Live event)
            $( 'form.checkout' ).on( 'change', ism, function() {
                if( $(ismc).val() == pickPoint )
                {
                    showHide('show',b1);
                    showHide('hide',b2);
                    showHide('hide',b3);
                    showHide('hide',b4);
                    showHide('hide',b5);

                    if( $(csa).prop('checked') ) {
                        showHide('show',s1);
                        showHide('hide',s2);
                        showHide('hide',s3);
                        showHide('hide',s4);
                        showHide('hide',s5);
                    }
                }
                else if( $(ismc).val() == localPickup )
                {
                    showHide('hide',b1);
                    showHide('show',b2);
                    showHide('show',b3);
                    showHide('hide',b4);
                    showHide('hide',b5);

                    if( $(csa).prop('checked') ) {
                        showHide('hide',s1);
                        showHide('show',s2);
                        showHide('show',s3);
                        showHide('hide',s4);
                        showHide('hide',s5);
                    }
                }
                else
                {
                    showHide('show',b1);
                    showHide('show',b2);
                    showHide('show',b3);
                    showHide('show',b4);
                    showHide('show',b5);

                    if( $(csa).prop('checked') ) {
                        showHide('show',s1);
                        showHide('show',s2);
                        showHide('show',s3);
                        showHide('show',s4);
                        showHide('show',s5);
                    }
                }
            });

            // When "shipping to different address" is changed (Live event)
            $(csa).click( function() {
                if( $(ismc).val() == pickPoint && $(this).prop('checked') )
                {
                    showHide('show',b1);
                    showHide('hide',b2);
                    showHide('hide',b3);
                    showHide('hide',b4);
                    showHide('hide',b4);

                    showHide('show',s1);
                    showHide('hide',s2);
                    showHide('hide',s3);
                    showHide('hide',s4);
                    showHide('hide',s5);
                }
                else if( $(ismc).val() == localPickup && $(this).prop('checked') )
                {
                    showHide('hide',b1);
                    showHide('show',b2);
                    showHide('show',b3);
                    showHide('hide',b4);
                    showHide('hide',b4);

                    showHide('hide',s1);
                    showHide('show',s2);
                    showHide('show',s3);
                    showHide('hide',s4);
                    showHide('hide',s5);
                }
                else
                {
                    showHide('show',b1);
                    showHide('show',b2);
                    showHide('show',b3);
                    showHide('show',b4);
                    showHide('show',b4);

                    showHide('show',s1);
                    showHide('show',s2);
                    showHide('show',s3);
                    showHide('show',s4);
                    showHide('show',s5);
                }
            });
        });
    </script>
    <?php
}

// Checkout conditional fields validation
add_action('woocommerce_checkout_process', 'wps_select_checkout_field_process');
function wps_select_checkout_field_process() {
    // HERE your shipping methods rate IDs
    $local_pickup = 'local_pickup:20';
    $pickpoint    = 'wc_custom_shipping_pickpoint';


    $chosen_shipping_method = WC()->session->get( 'chosen_shipping_methods' )[0];
    $billing                = '<strong> ' . __('Billing', 'woocommerce') . ' ';
    $shipping               = '<strong> ' . __('Shipping', 'woocommerce') . ' ';
    $country                = __('country.', 'woocommerce');
    $address1               = __('address.', 'woocommerce');
    $postcode               = __('postcode.', 'woocommerce');
    $state                  = __('state.', 'woocommerce');
    $end_text               = '</strong> '. __('is a required field.', 'woocommerce');

    if( $chosen_shipping_method == $local_pickup ) {
        if( empty($_POST['billing_address_1']) )
            wc_add_notice( $billing . $address1 . $end_text, 'error' );

        if( $_POST['ship_to_different_address'] ){
            if( empty($_POST['shipping_address_1']) )
                wc_add_notice( $shipping . $address1 . $end_text, 'error' );
        }
    }
    elseif( $chosen_shipping_method == $pickpoint ) {
        if( empty($_POST['billing_country']) )
            wc_add_notice( $billing . $country . $end_text, 'error' );

        if( $_POST['ship_to_different_address'] ){
            if( empty($_POST['shipping_country']) )
                wc_add_notice( $shipping . $country . $end_text, 'error' );
        }
    }
    else {
        if( empty($_POST['billing_country']) )
            wc_add_notice( $billing . $country . $end_text, 'error' );

        if( empty($_POST['billing_address_1']) )
            wc_add_notice( $billing . $address1 . $end_text, 'error' );

        if( empty($_POST['billing_postcode']) )
            wc_add_notice( $billing . $postcode . $end_text, 'error' );

        if( empty($_POST['billing_state']) )
            wc_add_notice( $billing . $state . $end_text, 'error' );

        if( $_POST['ship_to_different_address'] ){
            if( empty($_POST['shipping_country']) )
                wc_add_notice( $shipping . $country . $end_text, 'error' );

            if( empty($_POST['shipping_address_1']) )
                wc_add_notice( $shipping . $address1 . $end_text, 'error' );

            if( empty($_POST['shipping_postcode']) )
                wc_add_notice( $shipping . $postcode . $end_text, 'error' );

            if( empty($_POST['shipping_state']) )
                wc_add_notice( $shipping . $state . $end_text, 'error' );
        }
    }
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This is tested and works on WooCommerce 3+

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • For me it's always making the address, postcode etc mandatory even though it isn't visible when the method is selected. – Abu Nooh Jun 10 '18 at 17:15
  • @AbuNooh Here in this answer, those fields need to be set as mandatory by the jQuery code. Everything was working fine before Woocommerce 3.4. Now WC 3.4 has added "(optional)" to the non required fields labels set by the argument `'required' => false` in php. I have found the way to remove that "(optional)", but a Woocommerce jQuery event is adding them back in Checkout page. So I don't have the solution yet for this particular problem introduced with WC 3.4.x. – LoicTheAztec Jun 10 '18 at 18:07
  • @AbuNooh Finally I got the right way to do it… Check my updated answer in your question. – LoicTheAztec Jun 10 '18 at 19:06
  • Is there not, 2021, a easier way to manage this? All that code just to unset a few fields based on shipping seems overkill? –  Mar 13 '21 at 14:45