Hiding all checkout fields for virtual products

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.

How to do it…

We'll have to add some code to our functions.php file in our theme or create a custom WooCommerce plugin.

  1. At the bottom of your theme's functions.php file, add the following code to change the checkout fields that are displayed to the user:
    // Remove unnecessary checkout fields 
    add_filter( 'woocommerce_checkout_fields' , 'woocommerce_cookbook_remove_checkout_fields' );
  2. The code here calls a function, but we haven't written it yet. We now need to write the function that alters the checkout fields. Add the following to the bottom of the file:
    // 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; 
    }
  3. Save the file and upload it to your site.

Now, when the cart contains exclusively virtual products, you should only see the name and e-mail fields.

How to do it…

How it works…

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.

There's more...

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset