<?php
/**
* WooCommerce: Remove unnecessary billing fields from Checkout page.
*
* Billing fields will display if cart total is greater than zero at time of
* loading the Checkout page. If coupon is added to bring total down to zero,
* billing fields will remain displayed unless page is also refreshed by user.
*
* Billing fields will always display if the order needs shipping information
* (i.e. at least one item in the cart is not a virtual product).
*
* @link https://gist.github.com/cliffordp/f509d9eb82fb57d54c207fbf957808fb
*
* @see WC_Countries::get_address_fields
*/
add_filter( 'woocommerce_billing_fields', 'cliff_woo_hide_unnecessary_billing_fields', 20 );
function cliff_woo_hide_unnecessary_billing_fields( $address_fields ) {
if (
! function_exists( 'is_checkout' )
|| ! is_checkout()
|| ! is_array( $address_fields )
) {
return $address_fields;
}
global $woocommerce;
$cart_total = wc_prices_include_tax()
? $woocommerce->cart->get_cart_contents_total() + $woocommerce->cart->get_cart_contents_tax()
: $woocommerce->cart->get_cart_contents_total();
$cart_total = (int) $cart_total;
// Don't remove any fields if
if (
// cart total is greater than zero
0 < $cart_total
// or we need to ship something (i.e. not virtual product)
|| $woocommerce->cart->needs_shipping()
) {
return $address_fields;
}
/**
* Remove all default billing fields except First Name, Last Name, Phone,
* and Email address.
*
* "Additional information Order notes" field will display because it is not
* a billing field.
*/
unset( $address_fields['billing_country'] );
unset( $address_fields['billing_company'] );
unset( $address_fields['billing_address_1'] );
unset( $address_fields['billing_address_2'] );
unset( $address_fields['billing_city'] );
unset( $address_fields['billing_state'] );
unset( $address_fields['billing_postcode'] );
return $address_fields;
}