Add the following code to your theme’s functions.php to remove billing address fields from checkout form.
add_filter('woocommerce_checkout_fields','bt_custom_checkout_fields');
function bt_custom_checkout_fields( $fields ) {
unset($fields['billing']['billing_first_name']);
unset($fields['billing']['billing_last_name']);
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']);
return $fields;
}
Remove Billing Address Fields from WooCommerce Checkout Form if there are only virtual products in the cart
Add the following code to your theme’s functions.php to remove billing address fields if there are only virtual / digital / downloadable products in the cart.
add_filter('woocommerce_checkout_fields','bt_custom_checkout_fields');
function bt_custom_checkout_fields( $fields ) {
if( woo_cart_has_virtual_product() == true ) { //helper function to check if there are only virtual products in the cart
unset($fields['billing']['billing_first_name']);
unset($fields['billing']['billing_last_name']);
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']);
}
return $fields;
}
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;
}
Leave a comment in case you need help with the code snippets or WooCommerce customization and I will be happy to help you with it.
Remove Billing Address Fields from WooCommerce Checkout Form
Reviewed by webmission
on
19:34
Rating:
No comments:
Post a Comment