Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bolderelements/caf09b527e1d42b3992c5a118729f0d7 to your computer and use it in GitHub Desktop.
Save bolderelements/caf09b527e1d42b3992c5a118729f0d7 to your computer and use it in GitHub Desktop.
Remove the billing address fields for free virtual orders in WooCommerce
<?php
/**
* Plugin Name: WooCommerce Remove Billing Fields for Free Virtual Products
* Plugin URI: https://gist.github.com/bolderelements/caf09b527e1d42b3992c5a118729f0d7
* Description: Remove the billing address fields for free virtual orders, with the exception of email addresses
* Author: Erica Dion
* Author URI: http://bolderelements.net/
* Version: 1.0
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
function wc_remove_billing_fields( $fields ) {
global $woocommerce;
// if the total is more than 0 then we still need the fields
if ( 0 != $woocommerce->cart->total ) {
return $fields;
}
// return the regular billing fields if we need shipping fields
if ( $woocommerce->cart->needs_shipping() ) {
return $fields;
}
// we don't need the billing fields so empty all of them except the email
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']);
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_last_name']);
unset($fields['billing']['billing_city']);
return $fields;
}
add_filter( 'woocommerce_billing_fields', 'wc_remove_billing_fields', 20 );
@boofasten
Copy link

Hi Erica -

Thanks so much for forking this. Is this working for you? I'm still seeing billing options for free orders.

Thanks and cheers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment