Hide Checkout Fields Based on the Shipping Method: WooCommerce

In this article, we find out how you can hide checkout fields based on the Shipping methods.

The Code given below works for the following plugins :

 

Sometimes, you don’t want to display the default checkout options for your customers on your WooCommerce store. Like for certain products, you don’t want to show the billing addresses in the checkout option on your WooCommerce store. Or sometimes, when you have particular shipping options on your cart, you want to hide the shipping option.

Hide Checkout fields on your WooCommerce store || Code Snippet || WooCommerce || ELEX

The below code helps to hide the checkout field when a particular shipping option is selected. All you need to do is copy the code and paste it in Appearance>> Editor>>functions.php file.

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;
}

Here the above code will hide the checkout option when the “Free Shipping” option is selected in the shipping option.  Using the code, the billing address 1, as well as 2, will be hidden from the checkout page.

Hide Checkout fields on your WooCommerce store || Code Snippet || WooCommerce || ELEX

To Wrap Up,

You can make the changes to the code, with respect to the shipping methods you are using for your  WooCommerce store. If you are using any external shipping plugins, other than the default WooCommerce shipping methods, you can customize the code and use it. 

    Leave a Reply

    WordPress Image Lightbox Plugin