Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WooCommerce: Add Privacy Policy checkbox to checkout form
/**
* Add a privacy policy checkbox on the checkout form
*
* @author Wil Brown zeropointdevelopment.com
*/
function zpd_add_checkout_privacy_policy() {
woocommerce_form_field( 'privacy_policy', array(
'type' => 'checkbox',
'class' => array('form-row privacy'),
'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,
'label' => 'I have read and agree with the <a href="' . get_privacy_policy_url() . '">Privacy Policy</a>',
));
}
add_action( 'woocommerce_review_order_before_submit', 'zpd_add_checkout_privacy_policy', 9 );
// Show notice if customer doesn't check the box (required)
/**
* Show notice if customer doesn't check the new privacy policy checkbox
*
* @author Wil Brown zeropointdevelopment.com
*/
function zpd_privacy_not_agreed() {
if ( ! (int) isset( $_POST['privacy_policy'] ) ) {
wc_add_notice( __( 'Please read and accept the <strong>privacy policy</strong> to proceed with your order.' ), 'error' );
}
}
add_action( 'woocommerce_checkout_process', 'zpd_privacy_not_agreed' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.