WooCommerce Code Snippet — How To Remove Billing Address Fields For Virtual Products

Woocommerce code snippet - remove billing fields

Woocommerce allows you to sell physical as well as digital products. As a result there are several use cases where the checkout process is not optimized for user experience. Here are two such cases:

Use Case One: You are selling downloadable ebooks in your online store. When the user adds the ebooks to the cart and proceeds to checkout, he is asked to fill in the billing details. Ideally, digital products are virtual in nature and need to be delivered to the inbox (and not to any physical location). It simply doesn’t make sense to include billing address fields in the checkout form.

Use Case Two: This is actually one of the feature requested by my client selling hard books (physical product) as well as downloadable ebooks (virtual product). From user experience point of view, she wanted to make sure that if a user is purchasing only ebooks (downloadable product), then he should not be asked to fill in the billing details. In other words, at the time of checkout, the user should be asked to fill in the billing details only if he has added hard book (physical product) to the cart. This is one of the techniques to optimize conversions for your ecommerce website.

Here are code snippets for both the purposes.

  1. Remove billing address fields from checkout form (generic case).
  2. Remove billing address fields from checkout form if there are only virtual products in the cart.

You will need to edit your theme’s functions.php to add the following code snippet(s).

Reference: How to edit functions.php in WordPress

Remove Billing Address Fields from WooCommerce Checkout Form

Add the following code to your theme’s functions.php to remove billing address fields from checkout form.

add_filter('woocommerce_checkout_fields','bt_custom_checkout_fields');

function bt_custom_checkout_fields( $fields ) {
	unset($fields['billing']['billing_first_name']);
	unset($fields['billing']['billing_last_name']);
	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;
}

Reference: WooCommerce Checkout Fields

Remove Billing Address Fields from WooCommerce Checkout Form if there are only virtual products in the cart

Add the following code to your theme’s functions.php to remove billing address fields if there are only virtual / digital / downloadable products in the cart.

add_filter('woocommerce_checkout_fields','bt_custom_checkout_fields');

function bt_custom_checkout_fields( $fields ) {
	if( woo_cart_has_virtual_product() == true ) { //helper function to check if there are only virtual products in the cart
		unset($fields['billing']['billing_first_name']);
		unset($fields['billing']['billing_last_name']);
		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;
}


function woo_cart_has_virtual_product() {
	global $woocommerce;
	
	// By default, no virtual product
	$has_virtual_products = false;
	
	// Default virtual products number
	$virtual_products = 0;
	
	// Get all products in cart
	$products = $woocommerce->cart->get_cart();
	
	// Loop through cart products
	foreach( $products as $product ) {
		// Get product ID and '_virtual' post meta
		$product_id = $product['product_id'];
		$is_virtual = get_post_meta( $product_id, '_virtual', true );
		// Update $has_virtual_product if product is virtual
		if( $is_virtual == 'yes' ) {
			$virtual_products += 1;
		}
	}
	if( count($products) == $virtual_products ) {
		$has_virtual_products = true;
		}
	return $has_virtual_products;
}

Leave a comment in case you need help with the code snippets or WooCommerce customization and I will be happy to help you with it.

Divi WordPress Theme