6

So, I want to unset a few billing fields depending on shipping method, however it doesn't work unless I reload the whole checkout page. How to reload the billing fields using AJAX?

/*
 * Remove fields for chosen shipping method
 */
add_filter( 'woocommerce_checkout_fields', 'awoohc_override_checkout_fields' );
function awoohc_override_checkout_fields( $fields ) {

$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );

if ( 'local_pickup:4' === $chosen_methods[0] ) {

  unset( $fields['billing']['billing_address_1'] );
  unset( $fields['billing']['billing_address_2'] );


}

return $fields;
}
Vit Kashchuk
  • 71
  • 1
  • 1
  • 3

1 Answers1

0

Have a look at how we did it for removing shipping for "local pickup" methods in https://github.com/bakkbone/bakkbone-florist-companion/blob/trunk/incl/pickup.php

Takes a couple of functions:

  • one to unset the fields so the validation on submit doesn't fail (esp. if they're required fields) - you're on the right track with this with how you've done it
  • one to insert the javascript to hide the actual fields when the shipping method is changed

You're welcome to take as much of our code (word-for-word or modified) as you need :)