Last active
March 10, 2022 00:36
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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