1.4K
Introduction:
WooCommerce is a free and open-source e-commerce plugin for WordPress. It is designed to be easy to use and has a large number of features. WooCommerce has been around since 2011, so it’s been around for a while. There are over 1 million active installations on the web.
Add a Required Checkbox Field in the WooCommerce Checkout Page
Once you have decided where to include checkbox, basically for accepting terms and conditions, you need to find a relevant action where you want to hook fields. In this example, we used `woocommerce_review_order_before_submit` action to hook our function `labotrees_add_checkout_checkbox’. To add field we are using woocommerce inbuilt function `woocommerce_form_field` and pass parameters. This function accepts three parameters, $key, $args, and $value =null. You can find more information in example code below. This function will add terms and conditions checkbox on checkout page just before Proceed to Payment
/** * We are are trying to add checkboxes before proceed to make payment, you can use any action you want */
add_action( 'woocommerce_review_order_before_submit', 'labotrees_add_checkout_checkbox', 10 );
/** * Add WooCommerce additional Checkbox checkout field */
function labotrees_add_checkout_checkbox() {
woocommerce_form_field( 'checkout_checkbox',
array(
// CSS ID
'type' => 'checkbox',
'class' => array('form-row mycheckbox'), // CSS Class
'label_class' => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
'input_class' => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'),
'required' => true, // Mandatory or Optional
'label' => 'Ok I agree with terms and conditions.', // Label and Link
)
);
}
/** * Next thing we need to make it sure that we send a notice to woocommerce noitces if the checkbox is not selected
* `woocommerce_checkout_process` is appropraite action will do this trick
*/
add_action( 'woocommerce_checkout_process', 'bt_add_checkout_checkbox_warning' );
function bt_add_checkout_checkbox_warning() {
if ( ! (int) isset( $_POST['checkout_checkbox'] ) )
{
wc_add_notice( __( 'Please acknowledge the Checkbox' ), 'error' );
}
}
You Might Be Interested In
- How to smooth scroll to an id after clicking a link from another page?
- How to run jQuery after WooCommerce AJAX cart updated?
- How to change Address Format For a Specific Country in Woocommerce?
- How to use PHP Explode() function?
- How to get category for product page? | WooCommerce
- How to check the role of Current User ?
Post Views: 1,695