Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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/BFTrick/7873168
* Description: Remove the billing address fields for free virtual orders
* Author: Patrick Rauland
* Author URI: http://patrickrauland.com/
* Version: 2.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/>.
*
* @author Patrick Rauland
* @since 1.0
*/
function patricks_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_country'] );
unset( $fields['billing_first_name'] );
unset( $fields['billing_last_name'] );
unset( $fields['billing_company'] );
unset( $fields['billing_address_1'] );
unset( $fields['billing_address_2'] );
unset( $fields['billing_city'] );
unset( $fields['billing_state'] );
unset( $fields['billing_postcode'] );
unset( $fields['billing_phone'] );
return $fields;
}
add_filter( 'woocommerce_billing_fields', 'patricks_billing_fields', 20 );
// That's all folks!
@mattie02

This comment has been minimized.

Copy link

@mattie02 mattie02 commented Jan 3, 2014

Thanks man this was a great start to what i was looking for.

@nickolaso

This comment has been minimized.

Copy link

@nickolaso nickolaso commented Mar 7, 2014

this is perfect

@BFTrick

This comment has been minimized.

Copy link
Owner Author

@BFTrick BFTrick commented Apr 25, 2014

Glad you guys like it! I changed Billing Address to Billing Details which changed with WooCommerce 2.1

@BFTrick

This comment has been minimized.

Copy link
Owner Author

@BFTrick BFTrick commented Apr 25, 2014

Just updated to V2.0. I've changed this plugin to remove all of the fields except the email field. See more here: http://speakinginbytes.com/2013/12/remove-billing-address/#comment-34595

@maddisondesigns

This comment has been minimized.

Copy link

@maddisondesigns maddisondesigns commented Jul 12, 2014

Thanks for this. Just what I was after. Works beautifully! :-)

@modrod

This comment has been minimized.

Copy link

@modrod modrod commented Oct 16, 2014

thank you! is there a way to modify the code, so that it removes fields for orders that are not free (any amount)? if ( 0 != $woocommerce->cart->total ) { For example if I change 0 to 1, then any order of $1 will not show the fields. Is there a way to make it any order total greater than 0, will not show fields?

@mistermark

This comment has been minimized.

Copy link

@mistermark mistermark commented Nov 19, 2014

Definitely a great addition for WooCommerce. But shouldn't this be resolved on their end?

@omarel1

This comment has been minimized.

Copy link

@omarel1 omarel1 commented Mar 28, 2015

can you use this to remove billing fields for all virtual orders, even those that cost something? I don't see the need for the address at all.

@Smartyboy4you

This comment has been minimized.

Copy link

@Smartyboy4you Smartyboy4you commented Mar 28, 2015

I am very new to wordpress and woocommerce can someone tell me where exactly i should add that script so it has an effect? I added it in the plugin folder but nothing happend. All the fields are still there

@wouters

This comment has been minimized.

Copy link

@wouters wouters commented Apr 12, 2015

He Guys!
After updateing woocommerce I get:

Country is a required field.
First Name is a required field.
Last Name is a required field.
Address is a required field.
Town / City is a required field.
Postcode / Zip is a required field.
Phone is a required field.

anyone else having this problem?

@fliptpictures

This comment has been minimized.

Copy link

@fliptpictures fliptpictures commented Apr 15, 2015

I was having the same problem, so I added the code to the functions.php file of the theme and it worked that way.

@hughc

This comment has been minimized.

Copy link

@hughc hughc commented Apr 20, 2015

This is a bit of a sledgehammer I'm afraid. It has the potential to globally disable these fields, not just on checkout.

With the plugin enabled, and nothing in the cart, I've got no billing fields showing anywhere, including in the user profile area. I can understnad that the caort total would be zero, in this case, but it's not really the correct trigger.

It really needs to be limited to the checkout process; suspect it needs to use the
woocommerce_checkout_fields hook instead, to only trigger when the user is checking out?

@onigetoc

This comment has been minimized.

Copy link

@onigetoc onigetoc commented Dec 14, 2015

Hi, it seem to not work anymore with new Woocommerce version or maybe with new WordPress version 4.4
I found this one and it's work.
http://www.wpmayor.com/how-to-remove-the-billing-details-from-woocommerce-checkout/

@Bhimrao

This comment has been minimized.

Copy link

@Bhimrao Bhimrao commented Dec 29, 2015

guys, where should i place this file

@mrgranados

This comment has been minimized.

Copy link

@mrgranados mrgranados commented Jan 7, 2016

Hi, thank you for sharing this code BFTrick. I was able to get it working on 4.4.1 by pasting Jean Galea's filter and function over at WP Mayor (see comment above) and inserting your zero total and shipping conditionals in the functions.php file. Unfortunately, applying the coupon in the checkout page requires a refresh of the page in order to unset the billing details.

@lsi-tech

This comment has been minimized.

Copy link

@lsi-tech lsi-tech commented Feb 26, 2016

Thank you, brlliant

@taylortyger

This comment has been minimized.

Copy link

@taylortyger taylortyger commented Mar 8, 2016

Anyone coming across this, woocommerce has changed the structure of the checkout field arrays.

You will now need to add ['billing'] to each unset like so:

unset( $fields['billing']['billing_country'] );
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_state'] );
unset( $fields['billing']['billing_postcode'] );
unset( $fields['billing']['billing_phone'] );

@mbennett127

This comment has been minimized.

Copy link

@mbennett127 mbennett127 commented Mar 15, 2016

Just what I needed! Thanks man!

@EdTechClasses

This comment has been minimized.

Copy link

@EdTechClasses EdTechClasses commented Oct 15, 2016

Hey. Thanks so much for this plugin, Patrick. Works wonderfully.

@jws29378

This comment has been minimized.

Copy link

@jws29378 jws29378 commented Nov 23, 2016

This seems to have stopped working with the latest version of WC, with all orders now not displaying billing fields.

@contemplate

This comment has been minimized.

Copy link

@contemplate contemplate commented Jan 31, 2017

@jws29378 I had the same issue but then realized I had to have Shipping Zones & Methods setup first

@javad2625

This comment has been minimized.

Copy link

@javad2625 javad2625 commented Mar 20, 2017

Hello
I have exactly the same problem. When the user has only virtual products in their shopping basket, they have to enter their full address. This is absolutely unnecessary and naive.
What should I do?

Thank you

@ladydekade

This comment has been minimized.

Copy link

@ladydekade ladydekade commented Mar 28, 2017

Super handy! Nice work :)

@dragangurinovic

This comment has been minimized.

Copy link

@dragangurinovic dragangurinovic commented Jul 8, 2017

This is great but I have a problem, I can't remove address and postcode fields,
I used
unset($fields['billing']['billing_address_1']); unset($fields['billing']['billing_postcode']);

and it won't work. It does work for other fields.

@aaron843

This comment has been minimized.

Copy link

@aaron843 aaron843 commented Aug 11, 2017

Any updates to this for WC 3.x?

@bkozlowski

This comment has been minimized.

Copy link

@bkozlowski bkozlowski commented Nov 7, 2017

@BFTrick - There appears to be a bug with the latest version of WooCommerce 3.x. Even though billing fields are unset upon checkout WC still has the fields set to "require" somehow. When a customer completes checkout it shows an error that the unset fields are required.

Any ideas?

@nicholewagner1

This comment has been minimized.

Copy link

@nicholewagner1 nicholewagner1 commented Jan 4, 2018

The issue is with the conditional for needs payment:
if ( function_exists( 'is_checkout' ) && ( ! is_checkout() || ( is_checkout() && WC()->cart->needs_payment() ) ) ) {
return;
}
I commented this out and it works but then of course hides all the fields for all orders...

@michalmotai

This comment has been minimized.

Copy link

@michalmotai michalmotai commented Apr 14, 2018

if anyone figured it out. I would really appreciate some help.
I tried the previous codes. one stopped working the other eliminated all fields with all orders.
is there a solution?

@thehackermonkey

This comment has been minimized.

Copy link

@thehackermonkey thehackermonkey commented May 30, 2018

Using this worked for me on v 3.x+:

 unset($fields['order']['order_comments']);
 unset($fields['billing']['billing_postcode']);
 unset($fields['billing']['billing_state']);
 unset($fields['billing']['billing_address_1']);
 unset($fields['billing']['billing_address_2']);
 unset($fields['billing']['billing_country']);
 unset($fields['billing']['billing_city']);
 unset($fields['billing']['billing_company']);
@chwba

This comment has been minimized.

Copy link

@chwba chwba commented Jan 8, 2020

Very nice work. Im trying to change this, so that I can achieve to not ask for the billing address if someone pays via cryptocurrencies, anyone can hint me about how to proceed?

@lionelash

This comment has been minimized.

Copy link

@lionelash lionelash commented Aug 13, 2020

When I use this code, the checkout fields only get smaller, they aren't removed entirely:

unset($fields['billing_company']); unset($fields['billing_address_1']); unset($fields['billing_address_2']); unset($fields['billing_city']); unset($fields['billing_postcode']); unset($fields['billing_country']); unset($fields['billing_state']); unset($fields['billing_phone']);

I tried using:

unset($fields['order']['order_comments']); unset($fields['billing']['billing_postcode']); unset($fields['billing']['billing_state']); unset($fields['billing']['billing_address_1']); unset($fields['billing']['billing_address_2']); unset($fields['billing']['billing_country']); unset($fields['billing']['billing_city']); unset($fields['billing']['billing_company']);

But then the checkout fields remained at full size

Any idea what I can do, please?

Full Checkout Fields
Smaller Checkout Fields

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.