Add Multiple Required Checkbox Fields In WooCommerce Checkout Page
Following the Add A Required Checkbox Field In WooCommerce Checkout Page, this is the guide to adding multiple required checkboxes in the checkout.
See also: How to add checkbox to checkout in woocommerce (not required)
Add the following code to your theme’s functions.php file.
Please note that the first checkbox is an Age Confirmation (represened as “age_confirm” in the code) and the other one is Terms Of Use Agreement (represented as “tos_agreement”).
Change the texts in this code snippet according to your needs.
// Add Checkboxes add_action('woocommerce_after_checkout_billing_form', 'my_required_checkout_fields'); function my_required_checkout_fields() { woocommerce_form_field('age_confirm', array( 'type' => 'checkbox', 'class' => array('input-checkbox'), 'label' => __('I confirm that I am 21 years old or over.'), 'required' => true, ), WC()->checkout->get_value('age_confirm')); woocommerce_form_field('tos_agreement', array( 'type' => 'checkbox', 'class' => array('input-checkbox'), 'label' => __('I agree to the terms of use of this website.'), 'required' => true, ), WC()->checkout->get_value('tos_agreement')); } // Process the checkout add_action('woocommerce_checkout_process', 'my_custom_checkout_fields_process'); function my_custom_checkout_fields_process() { if (!$_POST['age_confirm']) { wc_add_notice(__('Please confirm you are 21 years old or over'), 'error'); } if (!$_POST['tos_agreement']) { wc_add_notice(__('Please agree to the terms of use first'), 'error'); } } //Update the order meta with field values add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_fields_update_order_meta'); function my_custom_checkout_fields_update_order_meta($order_id) { if ($_POST['age_confirm']) { update_post_meta($order_id, 'Age Confirm', esc_attr($_POST['age_confirm'])); } if ($_POST['tos_agreement']) { update_post_meta($order_id, 'Terms Of Use Agreement', esc_attr($_POST['tos_agreement'])); } }