-
-
Save BFTrick/7873168 to your computer and use it in GitHub Desktop.
<?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! |
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
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?
I was having the same problem, so I added the code to the functions.php file of the theme and it worked that way.
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?
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/
guys, where should i place this file
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.
Thank you, brlliant
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'] );
Just what I needed! Thanks man!
Hey. Thanks so much for this plugin, Patrick. Works wonderfully.
This seems to have stopped working with the latest version of WC, with all orders now not displaying billing fields.
@jws29378 I had the same issue but then realized I had to have Shipping Zones & Methods setup first
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
Super handy! Nice work :)
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.
Any updates to this for WC 3.x?
@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?
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...
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?
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']);
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?
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?
Hi, is there any new update code that also remove the field requirement ?
I've been using this code for almost 10 years now (see my original comment above), and it's been working great. I'm not sure if this is a recent issue or whether I've only just noticed, but this also stops people from being able to update their Billing Address details as it also removes these fields from the 'Edit Billing Address' page within your site's 'My Account' pages. To fix this, you just need to tweak the first if()
statement within the patricks_billing_fields()
function to check that you're not currently on an account page.
So... instead of the following:
if ( 0 != $woocommerce->cart->total ) {
replace it with the following:
if ( is_account_page() || 0 != $woocommerce->cart->total ) {
@maddisondesigns - Thanks for sharing this update! 🤓
@maddisondesigns - Thanks for sharing this update! 🤓
@BFTrick No probs. Thanks for this great code snippet. 🙂 It's kinda annoying that WooCommerce doesn't do this by default, for digital products.
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.