Skip to content

Instantly share code, notes, and snippets.

@everaldomatias
Created October 21, 2022 18:55
Show Gist options
  • Save everaldomatias/ec00332ed1b614a91051ca2e1486e51f to your computer and use it in GitHub Desktop.
Save everaldomatias/ec00332ed1b614a91051ca2e1486e51f to your computer and use it in GitHub Desktop.
Add checkbox to WooCommerce login
<?php
add_action( 'woocommerce_register_form', 'custom_add_registration_privacy_policy', 11 );
function custom_add_registration_privacy_policy() {
woocommerce_form_field( 'privacy_policy_reg', array(
'class' => array('form-row privacy'),
'input_class' => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'),
'label_class' => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
'label' => 'I\'ve read and accept the Privacy Policy',
'required' => true,
'type' => 'checkbox',
));
}
// Show error if user does not tick
add_filter( 'woocommerce_registration_errors', 'custom_validate_privacy_registration', 10, 3 );
function custom_validate_privacy_registration( $errors, $username, $email ) {
if ( ! is_checkout() ) {
if ( ! (int) isset( $_POST['privacy_policy_reg'] ) ) {
$errors->add( 'privacy_policy_reg_error', __( 'Privacy Policy consent is required!', 'woocommerce' ) );
}
}
return $errors;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment