This snippet could come really handy for several reasons. When you’ll need to do advanced customization of the checkout and its fields, hopefully you’ll thank me then ๐
In today’s snippet, we will add a new checkbox and another new “hidden” field – then, if the checkbox is ticked, the field will show, otherwise it will disappear again.
PHP Snippet: Add a Checkbox to Hide/Show a Custom Checkout Field
/**
* @snippet Add a Checkbox to Hide/Show Checkout Field - WooCommerce
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @compatible WC 4.1
* @community https://businessbloomer.com/club/
*/
add_filter( 'woocommerce_checkout_fields' , 'bbloomer_display_checkbox_and_new_checkout_field' );
function bbloomer_display_checkbox_and_new_checkout_field( $fields ) {
$fields['billing']['checkbox_trigger'] = array(
'type' => 'checkbox',
'label' => __('Checkbox label', 'woocommerce'),
'class' => array('form-row-wide'),
'clear' => true
);
$fields['billing']['new_billing_field'] = array(
'label' => __('New Billing Field Label', 'woocommerce'),
'placeholder' => _x('New Billing Field Placeholder', 'placeholder', 'woocommerce'),
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}
add_action( 'woocommerce_after_checkout_form', 'bbloomer_conditionally_hide_show_new_field', 9999 );
function bbloomer_conditionally_hide_show_new_field() {
wc_enqueue_js( "
jQuery('input#checkbox_trigger').change(function(){
if (! this.checked) {
// HIDE IF NOT CHECKED
jQuery('#new_billing_field_field').fadeOut();
jQuery('#new_billing_field_field input').val('');
} else {
// SHOW IF CHECKED
jQuery('#new_billing_field_field').fadeIn();
}
}).change();
");
}
Hey Rodolfo, this is a great snippet.
Thanks a lot!
Thanks!
Hi Rodolfo!
I’m using your code with a required field. When it’s not showing, WooCommerce still ask the user to complete it.
How to hide the field and also remove the “required”?
Thanks!
Ciao Luca, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hello there,
First of all, thank you very much.
How can I see the note written on the order edit page
Hi there, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hello Rodolfo.
Using your instructions I created a modified code to hide and display the Billing Details using a checkbox.
By default I have set so that the Billing Details are hidden.
The user can display them just by checking the Checkbox.
However, when a validation error occurs in the fields, such as the failure to fill in a mandatory field, the user receives the error message, but as the fields are hidden he may have difficulty locating the field, as he would have to check the checkbox to be able to see all the fields.
I’m trying, unsuccessfully, to have the checkbox automatically checked when an error occurs on this checkout page, but I’m not getting it.
Can you take a look at the code below?
Can you help-me with this?
Guilherme, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
hello @Rodolfo Melogli thank you for nice instruction how to add additional field based on checkbox(check/not).. i checked this code and get an error .. actually this field value doesn’t appear into order details page … i mean once i booked any order i could not see what customer typed into this field …can you kindly help me how can i show this field data into admin dashboard …Thanks in advance
Hi Saifur, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Rodolfo non so come ringraziarti…i tuoi insegnamenti sono i migliori del web…ti ringrazierรฒ sicuramente nei credits del mio ecommerce e del mio plugin, e la mia donazione la meriti davvero tutta.
Thank you / Grazie!
Hiya, Iโm unable to add This multiple times for multiple fields/boxes. Why is this?
Hi Elly, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hi,
Great approach, but it does the exact opposite as what you describe on top of the page:
” if the checkbox is ticked, the field will show, otherwise it will disappear again.”.
What it actually does, is hide a field on ticking on the checkbox (fadeOut), or show it when unticked.
I reversed the fadeIn with fadeOut, but then on pageload it gives a buggy result:
Showing the field at first, then disappearing.
I do assume you meant to make it work, like the default WC field ‘Create an account?’ works, right?
Else I cannot come up with any practical usecase for this.
Can you revise the code perhaps?
(this way it looks like clickbait in the description, surely that was not the purpose of this great snippet).
I wouldn’t call it clickbait. Anyway, just use “!this.checked” instead of “this.checked” and it’ll work the way you want. Cheers
Hi,
Thanks for sharing!
as No said, actually with “!this.checked” works, but when page loads you can see the field also if the checkbox it’s empty.
Therefore it still miss something to work as said..
Cheers
Sure. Just before:
write:
so thanks for this help
You’re welcome!
Hi, I have problem with label , why label is not hide with all field? No matters that checkbox is mark or no, label is always shows.
What’s the code you used exactly?
Yes, I have the same issue here. Used the original code.
If you used the exact same code, “new_billing_field_field” targets label + input so that’s weird. Maybe you need to adjust the JS according to your custom HTML
How to add multiple fields to show after check?
$fields[‘billing’][‘new_billing_field’] = array(
‘label’ => __(‘New Billing Field Label’, ‘woocommerce’),
‘placeholder’ => _x(‘New Billing Field Placeholder’, ‘placeholder’, ‘woocommerce’),
‘class’ => array(‘form-row-wide’),
‘clear’ => true
);
here..
Just add another $fields[][] block
Adding a new field block doesn’t work
Share your full code please?
Thank you Rodolfo.
This is exactly what I was looking for.
Excellent
Hi Rodolfo,
Thank you for this snippet.
I have customised it to show only on click
Do you have any tips to make it show/hide the company name field based on the checkbox status? So basically change the following to show the already available company field
Hi Ioannis, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hi rodolfo, i’m trying to modify your snippet but i have no success.:(
1) i created a new field “cf_field”
2) if #billing_country value is different from ‘IT’ i want to hide dinamically
Marco, thanks so much for your comment! Unfortunately this looks like custom troubleshooting work and I cannot help here via the blog comments. Thanks a lot for your understanding! ~R
Hi, how can i move the new checkbox on the top ?
Thank u so mutch.
Ciao Daniele, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
Hello Rodolfo,
First of all thanks for this snippet. I want to move the checkbox to hook “woocommerce_before_checkout_billing_form”, but somehow the checkbox does not move to the required hook? Is this possible?
TYhanks,
Tim
Tim, thanks so much for your comment! Unfortunately this looks like custom troubleshooting work and I cannot help here via the blog comments. Thanks a lot for your understanding! ~R
Hi, thank you! I try to work with this all day but I does not work the way I want. Its posible to contact you? Regards, Jordi
Jordin, if you’d like to get a quote, feel free to contact me here. Thanks a lot
Hi! Great snippet, I’ve been trying to solve this problem for weeks. However, I need to modify it a bit and I’m having a hard time doing so. I need it to work this way – in the default situation the field won’t show at all, only the checkbox. When checkbox is checked, the field appears. Also, the input is not showing in the administration. Is there any way to fix this? Thanks a lot! ๐
Michal, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here on the blog. Thanks a lot for your understanding! ~R
Hi, i want to add a checkbox if a customer rewuires invoice. In that case (if it is checked by the customer) company field, profession field (a custom field) and vat field (already added by a vat plugin at checkout fields) would have to appear and be required. Can this be done with your code?
Hey Akis thanks for your comment! Yes, you will need to modify it, but my snippet shows you how to achieve what you need ๐
Hi Rodolfo, this is a great snippet. But if the initially hidden field would be required, I’d still get required field message. Is there a solution for a scenario with a required field?
Hey Rasti! If the field is required, why do you need to hide it?
Hi, its possible to start this hide, so not show the new field. Just when you check the new field will show. Thank you! I really need this function for my website. Regards.
Hey Jordi, thanks for your comment! Yes, of course, this is possible, you just need to customize my snippet. Sorry but I can’t help with custom work here via the blog comments. Thank you ๐