Created
October 25, 2022 19:06
-
-
Save everaldomatias/f3c8793d60e632a07fabc8b1249b519f to your computer and use it in GitHub Desktop.
Add checkbox on register WooCommerce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Add term and conditions check box on registration form | |
add_action( 'woocommerce_register_form', 'add_terms_and_conditions_to_registration', 20 ); | |
function add_terms_and_conditions_to_registration() { | |
if ( wc_get_page_id( 'terms' ) > 0 && is_account_page() ) { | |
?> | |
<p class="form-row terms wc-terms-and-conditions"> | |
<label class="woocommerce-form__label woocommerce-form__label-for-checkbox checkbox"> | |
<input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="terms" <?php checked( apply_filters( 'woocommerce_terms_is_checked_default', isset( $_POST['terms'] ) ), true ); ?> id="terms" /> <span><?php printf( __( 'I’ve read and accept the <a href="%s" target="_blank" class="woocommerce-terms-and-conditions-link">terms & conditions</a>', 'woocommerce' ), esc_url( wc_get_page_permalink( 'terms' ) ) ); ?></span> <span class="required">*</span> | |
</label> | |
<input type="hidden" name="terms-field" value="1" /> | |
</p> | |
<?php | |
} | |
} | |
// Validate required term and conditions check box | |
add_action( 'woocommerce_register_post', 'terms_and_conditions_validation', 20, 3 ); | |
function terms_and_conditions_validation( $username, $email, $validation_errors ) { | |
if ( ! isset( $_POST['terms'] ) ) | |
$validation_errors->add( 'terms_error', __( 'Terms and condition are not checked!', 'woocommerce' ) ); | |
return $validation_errors; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment