To sell visual products, you do not need to receive the customer’s postal address. In this post, you will learn how to remove unnecessary fields in WooCommerce for visual products.
Remove Checkout Fields for Virtual products without plugins
In the WordPress dashboard, go to Appearance ➡ Theme File Editor and copy the following code into the theme’s functions.php file and update it.
You must create a child theme before making any changes to functions.php
file. Otherwise, the applied changes will be lost after each update.
Create child theme in WordPress step by step [without plugin]
As an alternative method, you can use the Code Snippets plugin to insert your codes into WordPress.
/**
* Remove Checkout Fields for Virtual products in WooCommerce
* @WPCookie
*/
add_filter( 'woocommerce_checkout_fields', 'simplify_checkout_virtual' );
function 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;
}
By inserting the above code on the site when purchasing visual products, all fields related to the address will be hidden in the checkout page.
Delete any fields you need from the code above to add them back to the checkout page. For example, to add the phone field, delete the following line in the above code.
unset($fields['billing']['billing_phone']);
If this article is difficult for you to read in text, you can watch the video version below.