When you are selling digital goods, your billing invoice is minimal. The reason being there are fewer billing fields. So, you have a scope to simplify your customer billing experience. In this article, you will learn how to remove checkout billing fields if virtual product is at cart.
You can simply implement the below code to hide extra billing fields at the checkout page.
PHP Snippet: Remove Checkout Billing Fields if Virtual Product @ Cart
add_filter( 'woocommerce_checkout_fields' , 'phpsof_simplify_checkout_virtual' );
function phpsof_simplify_checkout_virtual( $fields ) {
$only_virtual = true;
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// Check if there are non-virtual products
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']);
add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
}
return $fields;
}
Where do you add this snippet?
So, you can smartly hide extra billing fields if virtual or downloadable product is at cart. Just add this PHP snippet at your child theme function.php file’s bottom. It should be placed before “?>”, if you have it there. Apart from this, CSS goes in your child theme style.css file.
Is this snippet still valid?
So, this is how you remove checkout billing fields if virtual product is at cart. It will restrain cart abandonment due to redirection to the product page. I have applied this code on the Storefront theme and WordPress friendly hosting PHP 7.3. Let me know if everything works as expected. Share it further if this snippet has saved your time.
Also Read, How to Remove Product Links at Cart Page in WooCommerce
WooCommerce Move or Remove Coupon @ Cart & Checkout page
Important links: WooCommerce – https://woocommerce.com/