When a product is set as a virtual product, how can I hide the detailed billing address fields? I used a plugin, but it doesn’t work.
This topic has 2 replies, 2 voices, and was last updated 1 months, 1 weeks ago ago by Luca Rossi
When a product is set as a virtual product, how can I hide the detailed billing address fields? I used a plugin, but it doesn’t work.
Hello @Wanan,
Do you mean that when checking out with a cart containing only virtual products, the billing address fields should be hidden?
Please try adding the following custom code to the functions.php file located in your child theme:
add_filter( 'woocommerce_checkout_fields', 'hide_billing_address_if_virtual_products' );
function hide_billing_address_if_virtual_products( $fields ) {
// Check if all products in the cart are virtual
$only_virtual = true;
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( ! $cart_item['data']->is_virtual() ) {
$only_virtual = false;
break;
}
}
// If all products are virtual, remove billing address fields
if ( $only_virtual ) {
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'] );
}
return $fields;
}
I hope this helps.
Best regards,
8Theme Team
You must be logged in to reply to this topic.Log in/Sign up