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>';
}


Was this helpful? Please buy me a coffee






This code should be added to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Code snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be wiped entirely when you update.

Share your thoughts

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