WooCommerce is pretty smart. If you only have virtual products such as memberships or downloadable products in the cart, WooCommerce won't ask for any shipping information. But some people want to take this a step further. They don't want all of the billing details—they only want the bare minimum information.
WooCommerce doesn't automatically remove these fields because some payment gateways require more billing information. If your payment gateway doesn't require billing details, then you could do this and make the checkout process a bit smoother. The best way to find out if your payment gateway allows this is to try the following code snippet and do a test purchase.
We'll have to add some code to our functions.php
file in our theme or create a custom WooCommerce plugin.
// Remove unnecessary checkout fields add_filter( 'woocommerce_checkout_fields' , 'woocommerce_cookbook_remove_checkout_fields' );
// remove unnecessary checkout fields function function woocommerce_cookbook_remove_checkout_fields( $fields ) { // check if the cart needs shipping if ( false == WC()->cart->needs_shipping() ) { // hide the billing fields unset($fields['billing']['billing_company']); unset($fields['billing']['billing_address_1']); unset($fields['billing']['billing_address_2']); unset($fields['billing']['billing_city']); unset($fields['billing']['billing_postcode']); unset($fields['billing']['billing_country']); unset($fields['billing']['billing_state']); unset($fields['billing']['billing_phone']); // hide the additional information section
add_filter( 'woocommerce_enable_order_notes_field', '__return_false' ); }
return $fields; }
Now, when the cart contains exclusively virtual products, you should only see the name and e-mail fields.
This code only does one thing—it checks to see if the cart requires shipping and then it unsets the unnecessary fields. You could remove some of those unset lines to keep some fields (for example, the phone field) if you need them.
A word of caution about removing checkout fields: the more information you can give the payment gateway, the easier it is for them to determine if purchases are legitimate. You could have an increase in fraud if you don't ask for billing details such as country, state, postcode, and so on. If you do have an increase in fraud, start adding the fields back in or use a payment gateway such as Pay With Amazon, which uses billing information that's already been verified on their end.