Skip to content

Instantly share code, notes, and snippets.

@mattpramschufer
Forked from corsonr/gist:7215762
Created December 11, 2017 18:21
Show Gist options
  • Save mattpramschufer/957039753ea7a0bc3344e2f817304156 to your computer and use it in GitHub Desktop.
Save mattpramschufer/957039753ea7a0bc3344e2f817304156 to your computer and use it in GitHub Desktop.
WooCommerce: Hide Checkout Fields For Virtual Products
<?php
add_filter( 'woocommerce_checkout_fields' , 'woo_remove_billing_checkout_fields' );
/**
* Remove unwanted checkout fields
*
* @return $fields array
*/
function woo_remove_billing_checkout_fields( $fields ) {
if( woo_cart_has_virtual_product() == 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']);
unset($fields['order']['order_comments']);
unset($fields['billing']['billing_address_2']);
unset($fields['billing']['billing_postcode']);
unset($fields['billing']['billing_company']);
unset($fields['billing']['billing_city']);
}
return $fields;
}
/**
* Check if the cart contains virtual product
*
* @return bool
*/
function woo_cart_has_virtual_product() {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if($cart_item['data']->is_virtual()){
return true;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment