Remove the billing address fields for virtual orders in WooCommerce

Good evening,

My bands website allows the user to buy physical and virtual/downloadable versions of our albums.

These are variable products and when the user just buys a downloadable version it seems that they still have to fill in their billing address which is annoying for them.

I tried using the plugin with X theme ‘Woo Checkout Editor’ but it seems that you can only add rules to a product as a whole and not a variable of that product.

Ideally I want it so if they just buy a downloadable product the checkout is just entering their full name, email and ticking the terms and conditions

Login details and a good example of a variable product within the secure note

Thank you!
Jason

Hi there,

Thanks for writing in.

You can follow the customization mentioned here https://wpmayor.com/how-to-remove-the-billing-details-from-woocommerce-checkout/ and just remove the lines you don’t need. More extended sample is here https://gist.github.com/corsonr/7215762 where a condition of the virtual product is added. Unfortunately, we can’t provide support for this kind of customization, you may wish to consult it with a Woocommerce developer.

Thanks!

Good morning and thank you for this,

I have tried the second link and it seems that the system doesnt understand when a virtual item is in the cart. If I manually change this bit:

 // By default, no virtual product
  $has_virtual_products = false;

to

 // By default, no virtual product
  $has_virtual_products = true;

it works… Hmmmm dont know where to go with this

So second option:

Some of the albums are only digital so I thought I would use the plugin with X theme ‘Woo Checkout Editor’ and in a field condition I want to:

Show field only if - the cart does not contain a certain item. However I set this and the plugin flicks it to the other option “cart contains” when I save

Please advise
Thank you,
Jason

Hello Jason,

The Woo Checkout Editor will not work because you have a variable product. Proceed with the code in the first link added into your child theme’s function.php file.

add_filter( 'woocommerce_checkout_fields' , 'woo_remove_billing_checkout_fields' );
/**
 * Remove unwanted checkout fields
 *
 * @return $fields array
*/
function woo_remove_billing_checkout_fields( $fields ) {
    
    if( woo_cart_has_virtual_product() == true ) {
	    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']);
	    unset($fields['order']['order_comments']);
	    unset($fields['billing']['billing_address_2']);
	    unset($fields['billing']['billing_postcode']);
	    unset($fields['billing']['billing_company']);
	    unset($fields['billing']['billing_city']);
    }
    
    return $fields;
}
/**
 * Check if the cart contains virtual product
 *
 * @return bool
*/
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;
}

Add this first so we can test it. Please let us know how it goes.

Good morning and thank you for this

I have added this to the your child theme’s function.php and as far as I can tell everything is still visible.

I will keep the code in so you can test

Kind Regards,
Jason

Hi Jason,

We updated the code a bit so that the billing address will not display [ if all the products in the cart are Downloadable or Virtual ].

Please update the code with this new one in your child theme’s function.php.



add_filter( 'woocommerce_checkout_fields' , 'woo_remove_billing_checkout_fields' );
/**
 * Remove unwanted checkout fields
 *
 * @return $fields array
*/
function woo_remove_billing_checkout_fields( $fields ) {
  
    if( woo_cart_virtual_downloadable_product_only() == true ) {
	    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']);
	    unset($fields['order']['order_comments']);
	    unset($fields['billing']['billing_address_2']);
	    unset($fields['billing']['billing_postcode']);
	    unset($fields['billing']['billing_company']);
	    unset($fields['billing']['billing_city']);
    }
    
    return $fields;
}
/**
 * Check if the cart contains virtual/downloadable product only
 *
 * @return bool
*/
function woo_cart_virtual_downloadable_product_only() {
  
  global $woocommerce;
  
  // By default, virtual/downloadable product only
  $virtual_downloadable_products_only = true;
  
  
  // Get all products in cart
  $products = $woocommerce->cart->get_cart();
  
  // Loop through cart products
  foreach( $products as $product ) {


	  // Get product ID
	  $product_id = $product['product_id'];

	  // is variation downloadable
	  $is_downloadable = $product['data']->downloadable;

	  // is variation virtual
	  $is_virtual = $product['data']->virtual ;

	  
	  // Update $virtual_downloadable_products_only if product is not virtual or downloadable and exit loop
	  if( $is_virtual == 'no' && $is_downloadable == 'no' ){
		 $virtual_downloadable_products_only = false;
		 break;
	  }
  }
  
  return $virtual_downloadable_products_only;
}

We tried it and it works. Hope it works at your end too.

Thanks

1 Like

This is perfect thank you SO much for sending over this information

Excellent!!
Jason

You are most welcome. :slight_smile:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.