'Hide checkout fields when selecting multiple delivery methods in WooCommerce
I use code that hides the checkout fields if the Local Pickup shipping method is selected:
// 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:3';
$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', a6 = 'city',
b1 = b+a1+f, b2 = b+a2+f, b3 = b+a3+f, b4 = b+a4+f, b5 = b+a5+f, b6 = b+a6+f,
s1 = s+a1+f, s2 = s+a2+f, s3 = s+a3+f, s4 = s+a4+f, s5 = s+a5+f, s6 = s+a6+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('hide',b1 ); // Country
showHide('hide',b2 ); // Address 1
showHide('hide',b3 ); // Address 2
showHide('hide',b4 ); // Postcode
showHide('hide',b5 ); // State
showHide('hide',b6 ); // City
}
else if( $(ismc).val() == localPickup ) // Choosen "Local pickup" (Hidding "Take away")
{
showHide('hide',b1);
showHide('hide',b2);
showHide('hide',b3);
showHide('hide',b4);
showHide('hide',b5);
showHide('hide',b6);
}
else
{
showHide('hide',b1);
showHide('show',b2);
showHide('show',b3);
showHide('show',b4);
showHide('show',b5);
showHide('show',b6);
}
}, 100);
// When shipping method is changed (Live event)
$( 'form.checkout' ).on( 'change', ism, function() {
if( $(ismc).val() == pickPoint )
{
showHide('hide',b1);
showHide('hide',b2);
showHide('hide',b3);
showHide('hide',b4);
showHide('hide',b5);
showHide('hide',b6);
if( $(csa).prop('checked') ) {
showHide('hide',s1);
showHide('hide',s2);
showHide('hide',s3);
showHide('hide',s4);
showHide('hide',s5);
showHide('hide',s6);
}
}
else if( $(ismc).val() == localPickup )
{
showHide('hide',b1);
showHide('hide',b2);
showHide('hide',b3);
showHide('hide',b4);
showHide('hide',b5);
showHide('hide',b6);
if( $(csa).prop('checked') ) {
showHide('hide',s1);
showHide('hide',s2);
showHide('hide',s3);
showHide('hide',s4);
showHide('hide',s5);
showHide('hide',s6);
}
}
else
{
showHide('hide',b1);
showHide('show',b2);
showHide('show',b3);
showHide('show',b4);
showHide('show',b5);
showHide('show',b6);
if( $(csa).prop('checked') ) {
showHide('hide',s1);
showHide('show',s2);
showHide('show',s3);
showHide('show',s4);
showHide('show',s5);
showHide('hide',s6);
}
}
});
// When "shipping to different address" is changed (Live event)
$(csa).click( function() {
if( $(ismc).val() == pickPoint && $(this).prop('checked') )
{
showHide('hide',b1);
showHide('hide',b2);
showHide('hide',b3);
showHide('hide',b4);
showHide('hide',b4);
showHide('hide',b6);
showHide('hide',s1);
showHide('hide',s2);
showHide('hide',s3);
showHide('hide',s4);
showHide('hide',s5);
showHide('hide',s6);
}
else if( $(ismc).val() == localPickup && $(this).prop('checked') )
{
showHide('hide',b1);
showHide('hide',b2);
showHide('hide',b3);
showHide('hide',b4);
showHide('hide',b4);
showHide('hide',b6);
showHide('hide',s1);
showHide('hide',s2);
showHide('hide',s3);
showHide('hide',s4);
showHide('hide',s5);
showHide('hide',s6);
}
else
{
showHide('hide',b1);
showHide('show',b2);
showHide('show',b3);
showHide('show',b4);
showHide('show',b4);
showHide('show',b6);
showHide('hide',s1);
showHide('show',s2);
showHide('show',s3);
showHide('show',s4);
showHide('show',s5);
showHide('show',s6);
}
});
});
</script>
<?php
}
The problem is that I can't do this for multiple delivery methods. I need to specify local_pickup:3, local_pickup:6 and local_pickup:9. Hiding fields only works correctly with one specified delivery method.
Please help me to solve this problem. Thank you!
Solution 1:[1]
You need to define multiple delivery methods in the array.
Define array like this
$local_pickup = array( 'flat_rate:2','flat_rate:7' );
Now, where you compare $(ismc).val() == localPickup replace with below code. You have to find the selected method in the array so you can use includes.
localPickup.includes( $(ismc).val() )
Complete code.
// 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 = array('flat_rate:2','flat_rate:7');
$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',
a6 = 'city',
b1 = b+a1+f,
b2 = b+a2+f,
b3 = b+a3+f,
b4 = b+a4+f,
b5 = b+a5+f,
b6 = b+a6+f,
s1 = s+a1+f,
s2 = s+a2+f,
s3 = s+a3+f,
s4 = s+a4+f,
s5 = s+a5+f,
s6 = s+a6+f,
localPickup = '<?php echo json_encode($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( localPickup.includes( $(ismc).val() ) ){ // Choosen "Local pickup" (Hidding "Take away")
showHide('hide',b1);
showHide('hide',b2);
showHide('hide',b3);
showHide('hide',b4);
showHide('hide',b5);
showHide('hide',b6);
}else{
showHide('hide',b1);
showHide('show',b2);
showHide('show',b3);
showHide('show',b4);
showHide('show',b5);
showHide('show',b6);
}
}, 100);
// When shipping method is changed (Live event)
$( 'form.checkout' ).on( 'change', ism, function() {
if( localPickup.includes( $(ismc).val() ) ){
showHide('hide',b1);
showHide('hide',b2);
showHide('hide',b3);
showHide('hide',b4);
showHide('hide',b5);
showHide('hide',b6);
if( $(csa).prop('checked') ) {
showHide('hide',s1);
showHide('hide',s2);
showHide('hide',s3);
showHide('hide',s4);
showHide('hide',s5);
showHide('hide',s6);
}
}else{
showHide('hide',b1);
showHide('show',b2);
showHide('show',b3);
showHide('show',b4);
showHide('show',b5);
showHide('show',b6);
if( $(csa).prop('checked') ) {
showHide('hide',s1);
showHide('show',s2);
showHide('show',s3);
showHide('show',s4);
showHide('show',s5);
showHide('hide',s6);
}
}
});
// When "shipping to different address" is changed (Live event)
$(csa).click( function() {
if( localPickup.includes( $(ismc).val() ) && $(this).prop('checked') ){
showHide('hide',b1);
showHide('hide',b2);
showHide('hide',b3);
showHide('hide',b4);
showHide('hide',b4);
showHide('hide',b6);
showHide('hide',s1);
showHide('hide',s2);
showHide('hide',s3);
showHide('hide',s4);
showHide('hide',s5);
showHide('hide',s6);
}else{
showHide('hide',b1);
showHide('show',b2);
showHide('show',b3);
showHide('show',b4);
showHide('show',b4);
showHide('show',b6);
showHide('hide',s1);
showHide('show',s2);
showHide('show',s3);
showHide('show',s4);
showHide('show',s5);
showHide('show',s6);
}
});
});
</script>
<?php
}
Tested and works
Solution 2:[2]
Please try below solution this solution is also a working on many criteria.
add_filter('woocommerce_checkout_fields', 'xa_remove_billing_checkout_fields');
function xa_remove_billing_checkout_fields($fields) {
$shipping_method ='free_shipping:1'; // 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_address_1']); // Add/change filed name to be hide
unset($fields['billing']['billing_address_2']);
}
return $fields;
}
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 | Bhautik |
| Solution 2 | RAJAN PANCHAL |

