Skip to content

Instantly share code, notes, and snippets.

@neilgee
Last active March 10, 2022 00:36
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save neilgee/89944ee2c7af7f3f2268 to your computer and use it in GitHub Desktop.
Save neilgee/89944ee2c7af7f3f2268 to your computer and use it in GitHub Desktop.
WooCommerce Remove Address Fields from checkout based on presence of virtual products in cart
<?php
add_filter( 'woocommerce_checkout_fields' , 'virtual_products_less_fields' );
/**
* WooCommerce Remove Address Fields from checkout based on presence of virtual products in cart
* @link https://www.skyverge.com/blog/checking-woocommerce-cart-contains-product-category/
* @link https://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/
* @link https://businessbloomer.com/woocommerce-hide-checkout-billing-fields-if-virtual-product-cart/
*/
function virtual_products_less_fields( $fields ) {
// set our flag to be true until we find a product that isn't virtual
$virtual_products = true;
// loop through our cart
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// Check if there are non-virtual products and if so make it false
if ( ! $cart_item['data']->is_virtual() ) $virtual_products = false;
}
// only unset fields if virtual_products is true so we have no physical products in the cart
if( $virtual_products===true) {
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']);
//Removes Additional Info title and Order Notes
add_filter( 'woocommerce_enable_order_notes_field', '__return_false',9999 );
}
return $fields;
}
<?php
add_filter('woocommerce_billing_fields','wpb_custom_billing_fields');
// remove some fields from billing form
// ref - https://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/
function wpb_custom_billing_fields( $fields = array() ) {
unset($fields['billing_company']);
unset($fields['billing_address_1']);
unset($fields['billing_address_2']);
unset($fields['billing_state']);
unset($fields['billing_city']);
unset($fields['billing_phone']);
unset($fields['billing_postcode']);
unset($fields['billing_country']);
return $fields;
}
<?php
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields_ek', 99 );
// Remove some fields from billing form
// Our hooked in function - $fields is passed via the filter!
// Get all the fields - https://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/
function custom_override_checkout_fields_ek( $fields ) {
unset($fields['billing']['billing_company']);
unset($fields['billing']['billing_address_1']);
unset($fields['billing']['billing_postcode']);
unset($fields['billing']['billing_state']);
return $fields;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment