Add a custom checkbox in your WooCommerce checkout page

If you’d like to add an additional, custom checkbox to your WooCommerce checkout page and then have the checkbox value printed on the admin edit order screen, then the following code will help you achieve this:

add_action( 'woocommerce_review_order_before_submit', 'rs_wc_custom_checkout_field' );
function rs_wc_custom_checkout_field() {
    echo '<div id="rs_wc_custom_checkout_field">';
 
    woocommerce_form_field( 'nocheck', array(
        'type'      => 'checkbox',
        'class'     => array('input-checkbox'),
        'label'     => __('Awesome checkbox'),
    ),  WC()->checkout->get_value( 'nocheck' ) );
    echo '</div>';
}
 
// Save the custom checkout field in the order meta
 
add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta', 10, 1 );
function custom_checkout_field_update_order_meta( $order_id ) {
 
    if ( ! empty( $_POST['nocheck'] ) )
        update_post_meta( $order_id, 'nocheck', $_POST['nocheck'] );
}
 
// Display the custom field result on the order edit page
 
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_custom_field_on_order_edit_pages', 10, 1 );
function display_custom_field_on_order_edit_pages( $order ){
    $nocheck = get_post_meta( $order->get_id(), 'nocheck', true );
    if( $nocheck == 1 )
        echo '<p><strong>Awesome checkbox: </strong> <span style="color:red;">Has been checked</span></p>';
    elseif( $nocheck == 0 )
        echo '<p><strong>Awesome checkbox: </strong> <span style="color:red;">Has not been checked</span></p>';
}

You can add this code to your site following the instructions here:

How to add custom code to your WooCommerce/WordPress site the right way

Have any feedback? Be sure to let me know here: Contact me

4 thoughts on “Add a custom checkbox in your WooCommerce checkout page

Share your thoughts

Your email address will not be published. Required fields are marked *