How To Hide / Remove Billing Address for Virtual Products in Woocommerce

by | Sep 2, 2018 | WooCommerce Tutorials

WooCommerce Guide: Hiding Checkout Billing Fields for Virtual Product at Checkout Page

When selling downloadable and virtual products using woocommerce,  you might find yourself in a situation where you don’t require your customers to enter a billing address. Woocommerce in itself doesn’t have a provision for doing this and if you happen to have no woocommerce coding experience, it might take you a while to figure it out.

There are woocommerce plugins that will help you to hide woocommerce billing address fields but I have found not a single one that gives you the control over which product types you want to remove woocommerce checkout fields for.

I strongly discourage excessive use of plugins, as this will considerably slow your WordPress website. Use the code below to remove billing address fields for virtual products at woocommerce checkout page.

Who is this guide for? Anyone who has the knowhow of how WordPress works. If you don’t want to make customizations on your own, we can fix this issue for you.

PHP Code To Remove Billing Fields in WooCommerce Checkout if Cart Has Virtual Products

/**
 * Code Function:  Hides Woocommerce Billing Address During Checkout
 * Reference      https://www.smartwpfix.com
 * Author         Woocommerce Support @ smartwpfix.com
 * Woocommerce Compatibility:    Tested up-to version 3.4.4
 */
 
add_filter( 'woocommerce_checkout_fields' , 'smartwpfix_remove_billing_woo_virtual' );
 
function smartwpfix_remove_billing_woo_virtual( $fields ) {
     
    $only_virtual = true;
     
    foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        // Perform a check if the cart contains virtual items 
        if ( ! $cart_item['data']->is_virtual() ) $only_virtual = false; 
    }
     
    if( $only_virtual ) {
        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']);
    }
     
    return $fields;
}

//This code only hides the billing address if only the items added into the cart include a virtual item. 

Where to add the code to hide billing address for virtual products in woocommerce

Now you have the code you need to remove billing address fields if cart contains virtual products at Woocommerce checkout page. This code should be added to your theme’s functions.php file but unless you are using a child theme, I won’t recommend you edit your theme files as any changes are lost in the process of  updating your WordPress theme.

Recommended of inserting this code: Use any php snippets plugin to do this. PHP code snippets (Insert PHP) Free WordPress plugin works perfectly.

PS: Do not edit theme or woocommerce files if you don’t know what you are doing. 

Need Professional Help Removing Woocommerce Billing Address Fields From Your Store?

We are a WordPress Support Company and we can help you fix any WordPress / Woocommerce issue. How can we help? We offer:

1. WordPress Maintenance and Care Plans – We keep your website secure, fast, and healthy. The package that help you run your business with peace.

2. Woocommerce Support and Maintenance Packages, – (monthly or yearly plans) Specific for woocommerce customers.

3. One Time WordPress / Woocommerce Fixes ,- We charge $30.99 per support ticket. Each ticket fixes only one issue.
4. Improve WordPress Woocommerce Speed,- We optimize your website to load insanely fast.

0 Comments

WP Support From $30 Per Fix. Get Started

Maintenance By Smartwpfix.com