Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WooCommerce - Add checkbox field to the checkout
<?php
/**
* Add checkbox field to the checkout
**/
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');
function my_custom_checkout_field( $checkout ) {
echo '<div id="my-new-field"><h3>'.__('My Checkbox: ').'</h3>';
woocommerce_form_field( 'my_checkbox', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('I have read and agreed.'),
'required' => true,
), $checkout->get_value( 'my_checkbox' ));
echo '</div>';
}
/**
* Process the checkout
**/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
global $woocommerce;
// Check if set, if its not set add an error.
if (!$_POST['my_checkbox'])
$woocommerce->add_error( __('Please agree to my checkbox.') );
}
/**
* Update the order meta with field value
**/
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ($_POST['my_checkbox']) update_post_meta( $order_id, 'My Checkbox', esc_attr($_POST['my_checkbox']));
}
?>
@dunavista

This comment has been minimized.

Copy link

commented Jan 21, 2014

Hi
Which file do I add this to?
thx

@carazo

This comment has been minimized.

Copy link

commented Feb 26, 2014

In your functions.php of your theme.

@tmz1970

This comment has been minimized.

Copy link

commented Mar 11, 2014

I know the my_custom_checkout_field_process() makes the custom field required, but it does not show the typical red asterisk that other required fields have. How can that be done?

@tmz1970

This comment has been minimized.

Copy link

commented Mar 11, 2014

How is the custom field shown on the Order in WP-Admin? The my_custom_checkout_field_display_admin_order_meta() function (from http://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/) did not work for me.

@Mun33b

This comment has been minimized.

Copy link

commented Mar 29, 2014

Is it possible that i use the same code for my checkout page where i want additional pricings to be added automatically once chosen or ticked by a user? Lets say

I have a checkout with a digital product, and i want additional pricing to be added for Add-ons that comes with it would it be possible to use this?

@bdnorris

This comment has been minimized.

Copy link

commented Jun 6, 2014

Thanks for this! Saved the day.

@karljames

This comment has been minimized.

Copy link

commented Jul 11, 2014

This is great but I would love to know what code I would include to make this check box appear when only certain categories of products are in the checkout.

@flexseth

This comment has been minimized.

Copy link

commented Aug 15, 2014

This functionality does work to provide a checkbox that is required to proceed, but it doesn't show that value on the back-end.

I'm looking for something similar and stumped, my goal is, checkbox that says "Add me to your mailing list."

Yes/No, save on Administrative back end. Doesn't necessarily have to be required.

Has anyone been able to do something like this?

@Tiaaas

This comment has been minimized.

Copy link

commented Nov 5, 2014

Thanks for the code. It´s working fine. How I can add 6 checkboxes among each other?

@trsprod

This comment has been minimized.

Copy link

commented Mar 17, 2015

Yes, its a very useful Plug-In. Thank you :-)

Short Question: If the checkbox is not clicked, instead of "Please agree to.." it says
"Fatal error: Call to undefined method WooCommerce::add_error() " and the offended line is "$woocommerce->add_error( __('Please agree to my checkbox.') );" ..... Any Ideas?? Thanks for any Help :-)

@dseegers

This comment has been minimized.

Copy link

commented Apr 30, 2015

Where in the function.php must I place this code. I only need a accepted checkbox for the terms of delivery etc 'I have read and agreed', very simple

@eduardo-marcolino

This comment has been minimized.

Copy link

commented Jun 13, 2015

Thanks for this! Just a little note: I had to change $woocommerce->add_error() to wc_add_notice('my error message', 'error' ); to make it work with WooCommerce 2.3.*

@domedome

This comment has been minimized.

Copy link

commented Aug 25, 2015

eduardo-marcolino fix for WC 2.3 and above work like a charm. Thanks man!

@kbc123

This comment has been minimized.

Copy link

commented Jan 9, 2016

add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );

function my_custom_checkout_field( $checkout ) {
global $woocommerce;
$contents = WC()->cart->cart_contents;
$productid = array();
if( $contents ) foreach ( $contents as $cart_item ){
$productid[] = $cart_item['product_id'];
}
echo "

";
woocommerce_form_field( 'digest_subscribe', array(
'type' => 'checkbox',
'class' => array('my-field-class form-row-wide'),
'label' => __('Please send me the Online Yoga Weekly newsletter with news links, yoga pose, and inspiration for my practice.'),
'default' => 1
), $checkout->get_value( 'digest_subscribe' ));

if(in_array('6614',$productid)){
echo "

Please update me about latest video in YogaLifeTV.

"; woocommerce_form_field( 'news_yltvmail', array( 'type' => 'checkbox', 'class' => array('input-checkbox'), 'label' => __('By Mail'), 'required' => true, 'default' => 1 ), $checkout->get_value( 'news_yltvmail' )); echo ""; woocommerce_form_field( 'news_yltvsms', array( 'type' => 'checkbox', 'class' => array('input-checkbox'), 'label' => __('By SMS'), 'readonly' => 'readonly', 'default' => 0 ) ,$checkout->get_value( 'news_yltvsms' )); echo "
"; } echo "
"; }

add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
global $woocommerce;
if (!$_POST['news_yltvsms'])
$woocommerce->add_error( __('Please agree to my checkbox.') );
}

add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');

function my_custom_checkout_field_update_order_meta( $order_id ) {
if ($_POST['news_yltvsms']) update_post_meta( $order_id, 'By SMS', esc_attr($_POST['news_yltvsms']));
}

we are
i have to add 2 check box in checkout page..code is function.php...so i want all customer check box by mail ya by sms ..and proceed make payment...my all update show my admin like check box field add by mail ya by sms...so please help me

@ladari333

This comment has been minimized.

Copy link

commented Jan 14, 2016

Can anyone help with this? I am trying to add a check box or radio button at checkout for the parent to choose which child they are registering for. The children information has been added to the parents profile using custom fields. Is there anyway for woocommerce to auto pull each child name so the parent can choose which child they are signing up and also have this displayed in the email thats sent out? Some parents may only have one kid some may have 4.

Thanks!

@OoButt222

This comment has been minimized.

Copy link

commented May 23, 2016

Can anyone help me on this?
I want to add a Send me invoice functionality to checkout page in such a way that if a user select Send Me Invoice and then he select a payment method Paypal the send me invoice checkbox keep selected and works

@SarahMouse

This comment has been minimized.

Copy link

commented Jul 12, 2016

Hi there,

I am having problem with this code. If I copy it in full it breaks my site. If I edit it to remove the error message piece of the code, it works on the front end, but I do not receive notification in the order notes.

Thanks if you can help!

@maltehelmhold

This comment has been minimized.

Copy link

commented Aug 8, 2016

also for me throwing:
SyntaxError: Unexpected token < in JSON at position 0

if it's not clicked. instead of "Please agree to my checkbox."

I can not find any other tutorial how to solve this task manually (without installing
an extra plugin)

@MikeHassall

This comment has been minimized.

Copy link

commented Aug 14, 2016

I was getting an internal server error with the code above after my latest update. Fixed by changing $woocommerce->add_error to wc_add_notice (see comment by eduardo-marcolino above).

@GamerDamian

This comment has been minimized.

Copy link

commented Aug 16, 2016

Hi,

i try change location checkbox to woocommerce_checkout_after_terms_and_conditions.

When i change:

* Add checkbox field to the checkout **/ add_action('**woocommerce_checkout_after_terms_and_conditions**', 'my_custom_checkout_field');

i have error:

Fatal error: Call to a member function get_value() on string in

in line:

), $checkout->get_value( 'my_checkbox' ));

What is wrong?

@DisobedientMedia

This comment has been minimized.

Copy link

commented Aug 23, 2016

@GamerDamian
woocommerce_checkout_after_terms_and_conditions does not pass in the $checkout variable, so you cant call a function on nil
see woocommerce hooks

@dmcanaveral

This comment has been minimized.

Copy link

commented Sep 9, 2016

If the checkbox isn't checked and throws an error, the form won't submit after it has been checked and the place order button is clicked again. It keeps throwing the error message. Any ideas?

@abaper123

This comment has been minimized.

Copy link

commented Oct 24, 2016

How to add some amount in cart total if the checkbox is checked?

@ZaheerAbbasAghani

This comment has been minimized.

Copy link

commented May 11, 2017

how can i add this checkbox after the payment methods ?

@zazikhan

This comment has been minimized.

Copy link

commented May 18, 2017

Hi there i am getting error on this add_action('woocommerce_checkout_fields', 'my_custom_checkout_field');

function my_custom_checkout_field( $checkout ) {
woocommerce_form_field( 'my_field_name', array( 
        'type'          => 'text', 
        'class'         => array('my-field-class form-row-wide'), 
        'label'         => __('Fill in this field'), 
        'placeholder'   => __('Enter a number'),
        ), $checkout->get_value( 'my_field_name' )); 

Fatal error: Call to a member function get_value() on array in C:\xampp\htdocs\flower\wp-content\themes\Avada-child\functions.php on line 286

What's wrong i am doing ?

@Sea-Wing-designs

This comment has been minimized.

Copy link

commented Jul 13, 2017

Hello, I have tried this code snippit and found that if I leave a required field blank, I get an Internal Server Error. I am wondering what I can do to clear this up? Here is the code in my functions.php:

/** Add checkbox field to the checkout
**/
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');

function my_custom_checkout_field( $checkout ) {

echo '<div id="my-new-field"><h3>'.__('Mail List: ').'</h3>';

woocommerce_form_field( 'my_checkbox', array(
    'type'          => 'checkbox',
    'class'         => array('input-checkbox'),
    'label'         => __('Please add my email address to your mail list to keep me up to date on future offerings.'),
    'required'  => false,
    ), $checkout->get_value( 'my_checkbox' ));

echo '</div>';

}

/**

  • Process the checkout
    **/
    add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {
global $woocommerce;

// Check if set, if its not set add an error.
if (!$_POST['my_checkbox'])
     $woocommerce->add_error( __('Would you like to join the mail list?') );

}

/**

  • Update the order meta with field value
    **/
    add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');

function my_custom_checkout_field_update_order_meta( $order_id ) {
if ($_POST['my_checkbox']) update_post_meta( $order_id, 'My Checkbox', esc_attr($_POST['my_checkbox']));
}

@MatLeduc

This comment has been minimized.

Copy link

commented Jul 20, 2017

Hi,

I get this Warning above the Place Order button just for a few seconds when the Checkout page is loading and then the warning disappear.

2017-07-20_direct-signature-warning

What can I do to fix this issue?

Thank you for your help!

@d3vit

This comment has been minimized.

Copy link

commented May 21, 2018

For those of you getting the error with get_value on an array -- use WC_Checkout::get_value instead

@hmbashar

This comment has been minimized.

Copy link

commented Jun 20, 2018

Working fine for checkbox, Thanks for the resource.

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.