2

in WooCommerce, I am trying to hide the company name field whenever "delivery to home" is selected. I've tried a bunch of different things.

This is my most recent attempt:

add_filter('woocommerce_checkout_fields', 'xa_remove_billing_checkout_fields');

function xa_remove_billing_checkout_fields($fields) {
    $shipping_method ='pakkelabels_shipping_gls1'; // Set the desired shipping method to hide the checkout field(s).
    global $woocommerce;
    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    $chosen_shipping = $chosen_methods[0];

    if ($chosen_shipping == $shipping_method) {
        unset($fields['billing']['billing_company']); // Add/change filed name to be hide
}
    return $fields;
}

But all it does is move the Shipping company from the first field to the last.

How can I conditionally hide a specific checkout field based on chosen shipping method?

starball
  • 20,030
  • 7
  • 43
  • 238
eMikkelsen
  • 407
  • 2
  • 13
  • 27

1 Answers1

5

As it's a live event, you need to use javascript/jQuery to make it work. The Billing company has to be not required like in default WooCommerce checkout page.

The following code will hide the "Billing company" field, when "Home delivery" shipping is chosen:

// Conditional Show hide checkout fields based on chosen shipping methods
add_action( 'wp_footer', 'conditionally_hidding_billing_company' );
function conditionally_hidding_billing_company(){
    // Only on checkout page
    if( ! ( is_checkout() && ! is_wc_endpoint_url() ) ) return;

    // HERE your shipping methods rate ID "Home delivery"
    $home_delivery = 'pakkelabels_shipping_gls1';
    ?>
    <script>
        jQuery(function($){
            // Choosen shipping method selectors slug
            var shipMethod = 'input[name^="shipping_method"]',
                shipMethodChecked = shipMethod+':checked';

            // Function that shows or hide imput select fields
            function showHide( actionToDo='show', selector='' ){
                if( actionToDo == 'show' )
                    $(selector).show( 200, function(){
                        $(this).addClass("validate-required");
                    });
                else
                    $(selector).hide( 200, function(){
                        $(this).removeClass("validate-required");
                    });
                $(selector).removeClass("woocommerce-validated");
                $(selector).removeClass("woocommerce-invalid woocommerce-invalid-required-field");
            }

            // Initialising: Hide if choosen shipping method is "Home delivery"
            if( $(shipMethodChecked).val() == '<?php echo $home_delivery; ?>' )
                showHide('hide','#billing_company_field' );

            // Live event (When shipping method is changed)
            $( 'form.checkout' ).on( 'change', shipMethod, function() {
                if( $(shipMethodChecked).val() == '<?php echo $home_delivery; ?>' )
                    showHide('hide','#billing_company_field');
                else
                    showHide('show','#billing_company_field');
            });
        });
    </script>
    <?php
}

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

Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • You are amazing. Just one last thing. The above code does everything it is supposed to, but we have three types of delivery, so I need it to work with one more type of delivery. Any chance you could let me know how to add that to it? I marked your answer as solving the problem. 1. To giftshop (the one you use there) 2. To home address flat_rate3 3. To company address. We need the above code to also hide the company field on value = flat_rate:3 / label = shipping_method_0_flate_rate3 How would we add that next to the pakkelabels_shipping_gls1, which is the giftshop one. – eMikkelsen Dec 11 '17 at 21:15
  • Or perhaps it is easier to say if it is not flat_rate:2 hide the billing company field? Flat rate 2 = our deliver to companies, the two others dont need the field. – eMikkelsen Dec 11 '17 at 21:39
  • 1
    @eMikkelsen Change the php `$home_delivery` variable value to `flat_rate:2` and in the jQuery code for the **`if`** statements (2 occurrences) replace `if( $(shipMethodChecked).val() ==` by `if( $(shipMethodChecked).val() != ` *(so you replace `==` by `!=` 2 times)*… it should work. – LoicTheAztec Dec 11 '17 at 21:48