Website Building » WooCommerce » How Do I Create a Checkbox Field in WooCommerce Checkout Page?

How Do I Create a Checkbox Field in WooCommerce Checkout Page?

Last updated on October 1, 2022 @ 4:43 pm

When you are running a WooCommerce store, you might need to add extra fields to the checkout page. For example, you might want to add a checkbox field that allows customers to opt-in to your newsletter.

In this article, we will show you how to add a checkbox field to the WooCommerce checkout page.

PRO TIP: When creating a checkbox field in the WooCommerce checkout page, be sure to check the “Required” option. This will ensure that the field is required in order to complete the checkout process.

Adding a checkbox field to the WooCommerce checkout page is very easy. First, you will need to add the following code to your theme’s functions.php file or a site-specific plugin:

// Add a custom 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">';
 
    WooCommerce_form_field( 'my_field_name', array(
        'type'          => 'checkbox',
        'class'         => array('my-field-class form-row-wide'),
        'label'         => __('Enable this option'),
        ), $checkout->get_value( 'my_field_name' ));
 
    echo '</div>';
 
}

Note: The code above goes in your theme’s functions.php file or a site-specific plugin.

Morgan Bash

Morgan Bash

Technology enthusiast and Co-Founder of Women Coders SF.