If you use WooCommerce to sell virtual products—such as paid digital downloads or access to resources—users can typically download their purchase immediately after payment. Since there is no physical shipping or billing invoice required for these transactions, the standard checkout fields for shipping and billing addresses are often redundant and can frustrate customers.
By removing these unnecessary address fields when a cart contains only virtual products, you can significantly reduce the amount of information a user needs to provide, thereby increasing checkout efficiency and conversion rates. The key to implementing this is accurately detecting if every item in the cart is virtual.
We can achieve this by iterating through the current cart items. If even a single item is marked as physical (non-virtual), the address fields must remain visible. Below is the code snippet to implement this logic:
add_filter('woocommerce_checkout_fields', function ($fields)
{
$only_virtual = true;
// Loop through cart items to check if any are non-virtual
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
if ( ! $cart_item['data']->is_virtual()) {
$only_virtual = false;
break; // Stop immediately if a physical product is found
}
}
// If only virtual products are present, remove address-related fields
if ($only_virtual) {
unset($fields['billing']['billing_first_name']);
unset($fields['billing']['billing_last_name']);
unset($fields['billing']['billing_email']);
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']);
// Also remove the order notes field
add_filter('woocommerce_enable_order_notes_field', '__return_false');
}
return $fields;
});
By applying this filter, you create a much cleaner and faster experience for your digital customers, ensuring they only provide the absolute minimum information required to complete their purchase.
