Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simplify WooCommerce checkout fields for free checkouts
<?php // only copy if needed
/**
* Removes coupon form, order notes, and several billing fields if the checkout doesn't require payment.
*
* Tutorial: http://skyver.ge/c
*/
function sv_free_checkout_fields() {
// first, bail if the cart needs payment, we don't want to do anything
if ( WC()->cart && WC()->cart->needs_payment() ) {
return;
}
// now continue only if we're at checkout
// is_checkout() was broken as of WC 3.2 in ajax context, double-check for is_ajax
// I would check WOOCOMMERCE_CHECKOUT but testing shows it's not set reliably
if ( function_exists( 'is_checkout' ) && ( is_checkout() || is_ajax() ) ) {
// remove coupon forms since why would you want a coupon for a free cart??
remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 );
// Remove the "Additional Info" order notes
add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
// Unset the fields we don't want in a free checkout
function unset_unwanted_checkout_fields( $fields ) {
// add or remove billing fields you do not want
// fields: http://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#section-2
$billing_keys = array(
'billing_company',
'billing_phone',
'billing_address_1',
'billing_address_2',
'billing_city',
'billing_postcode',
'billing_country',
'billing_state',
);
// unset each of those unwanted fields
foreach( $billing_keys as $key ) {
unset( $fields['billing'][ $key ] );
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'unset_unwanted_checkout_fields' );
}
}
add_action( 'wp', 'sv_free_checkout_fields' );
@bekarice

This comment has been minimized.

Show comment
Hide comment
@bekarice

bekarice Sep 2, 2015

Noticed there is one case this doesn't hold: If there's a signup fee but a free subscription, the fields also disappear, which we don't want since payment is due. Insert this after line 13 (the first check to bail out) to account for this scenario if you use free subscriptions with signup fees:

// Subscriptions check: bail Subs is active and we have an initial payment to charge
if ( class_exists( 'WC_Subscriptions' ) ) {
    $cart = WC()->cart;
    $cart_total = WC()->cart->total;

    if ( WC_Subscriptions_Cart::calculate_subscription_totals( $cart_total, $cart ) ) {
        return;
    }
}

final note: I don't really check comments here, just leaving this one since I don't want to update the main gist as this won't apply to most people.

Owner

bekarice commented Sep 2, 2015

Noticed there is one case this doesn't hold: If there's a signup fee but a free subscription, the fields also disappear, which we don't want since payment is due. Insert this after line 13 (the first check to bail out) to account for this scenario if you use free subscriptions with signup fees:

// Subscriptions check: bail Subs is active and we have an initial payment to charge
if ( class_exists( 'WC_Subscriptions' ) ) {
    $cart = WC()->cart;
    $cart_total = WC()->cart->total;

    if ( WC_Subscriptions_Cart::calculate_subscription_totals( $cart_total, $cart ) ) {
        return;
    }
}

final note: I don't really check comments here, just leaving this one since I don't want to update the main gist as this won't apply to most people.

@markob17

This comment has been minimized.

Show comment
Hide comment
@markob17

markob17 Oct 14, 2016

Hi Beka,

Thank you for the great code snippet. Do you have any idea how to not hide the fields when a subscription with a free trial is involved? I found this is another case where this doesn't hold. Woocommerce still hides the fields because it sees a subtotal of $0 because of the free subscription trial period.

Thanks!

Mark

markob17 commented Oct 14, 2016

Hi Beka,

Thank you for the great code snippet. Do you have any idea how to not hide the fields when a subscription with a free trial is involved? I found this is another case where this doesn't hold. Woocommerce still hides the fields because it sees a subtotal of $0 because of the free subscription trial period.

Thanks!

Mark

@BenLinders

This comment has been minimized.

Show comment
Hide comment
@BenLinders

BenLinders Oct 29, 2017

Hi Beka,

I've been using this code snippet long time to allow customers to download free articles

But now I see that when I use the snippet, woocommerce gives an error message that billing country, street/address, etc is required.

Has something changed in woocommerce that makes it fail?

Ben LInders

BenLinders commented Oct 29, 2017

Hi Beka,

I've been using this code snippet long time to allow customers to download free articles

But now I see that when I use the snippet, woocommerce gives an error message that billing country, street/address, etc is required.

Has something changed in woocommerce that makes it fail?

Ben LInders

@em-piguet

This comment has been minimized.

Show comment
Hide comment
@em-piguet

em-piguet Dec 21, 2017

Hi there, same problem here. I get errors on fields i have unset().. Any advice ?

em-piguet commented Dec 21, 2017

Hi there, same problem here. I get errors on fields i have unset().. Any advice ?

@merridennis

This comment has been minimized.

Show comment
Hide comment

merridennis commented Jan 1, 2018

@glasgowshipyard

This comment has been minimized.

Show comment
Hide comment
@glasgowshipyard

glasgowshipyard Feb 20, 2018

Hello! Seems this no longer works with woocommerce 3.3. unset fields are still required.

glasgowshipyard commented Feb 20, 2018

Hello! Seems this no longer works with woocommerce 3.3. unset fields are still required.

@wosc

This comment has been minimized.

Show comment
Hide comment
@wosc

wosc Sep 16, 2018

I'm running woocommerce 3.4, and as noted above, while the fields are properly omitted from the form, one then gets "field billing city is required" etc. errors when submitting the form. I solved this by modifying the main condition to include not only is_checkout but also is_ajax, as described here: https://stackoverflow.com/a/52089144/1885340.

wosc commented Sep 16, 2018

I'm running woocommerce 3.4, and as noted above, while the fields are properly omitted from the form, one then gets "field billing city is required" etc. errors when submitting the form. I solved this by modifying the main condition to include not only is_checkout but also is_ajax, as described here: https://stackoverflow.com/a/52089144/1885340.

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