-
-
Save bekarice/474ab82ab37b8de8617d to your computer and use it in GitHub Desktop.
<?php // only copy if needed | |
/** | |
* Removes coupon form, order notes, and several billing fields if the checkout doesn't require payment. | |
* | |
* REQUIRES PHP 5.3+ | |
* | |
* Tutorial: http://skyver.ge/c | |
*/ | |
function sv_free_checkout_fields() { | |
// first, bail if WC isn't active since we're hooked into a general WP hook | |
if ( ! function_exists( 'WC' ) ) { | |
return; | |
} | |
// 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 | |
add_filter( 'woocommerce_checkout_fields', function( $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_action( 'wp', 'sv_free_checkout_fields' ); |
I've been using the script for a day being completely happy (and my customers more!).
But I realised that the paid orders came only with billing_first_name, billing_second_name and billing_email.
I made sure that the checkout is requiring the rest of the fields for paid orders, and it does. So the user enters it but the system deletes them afterwards.
I believe that's because there's no payment needed when processing the order after the purchase is made, so the extra fields are deleted.
Anybody has got a solution?
Hi Beka,
Great snippet. It works perfectly but for one thing: Currently First Name and Last Name are optional and I wish they could be mandatory.
Prénom (facultatif)
Nom (facultatif)
Adresse de messagerie * Votre adresse courriel nous aide à améliorer votre expérience d'achat sur le site. Veuillez lire notre Politique de Confidentialité pour voir comment nous utilisons vos données personnelles. Vous pouvez désactiver le suivi du panier.
How can I manage this ?
Thank you very much in advance.
Regards,,
Max
Hi! is there a way to view all fields if a product is in a certain category and is free? thank you
Never mind, this kind of thing happens when you deactivate the woo commerce plugin in an attempt to problem solve wp-cron not working.