Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save xlplugins/5861fd681c7bc343f590ca0e873b26ba to your computer and use it in GitHub Desktop.

Select an option

Save xlplugins/5861fd681c7bc343f590ca0e873b26ba to your computer and use it in GitHub Desktop.
Confirm email fiedl for no logged in users only
class Create_Confirm_Email_Field {
public $email_required_msg = '';
public $email_not_matched = '';
public $display_page = false;
public function __construct() {
// Run this feature only for guest users
if ( is_user_logged_in() ) {
return;
}
add_action( 'wfacp_template_load', [ $this, 'action' ] );
add_action( 'wfacp_after_billing_email_field', [ $this, 'create_field' ] );
add_action( 'wfacp_internal_css', [ $this, 'internal_script' ] );
}
public function action() {
$this->display_page = true;
$this->email_required_msg = __( '<b>Confirm Email</b> is a required field.', 'woocommerce' );
$this->email_not_matched = __( '<b>Confirm Email</b> does not match.', 'woocommerce' );
add_action( 'woocommerce_after_checkout_validation', [ $this, 'matching_email_addresses' ], 8, 2 );
}
public function create_field() {
if ( false === $this->display_page ) {
return;
}
$field = array(
'label' => __( 'Confirm Email', 'woocommerce-email-validation' ),
'type' => 'email',
'field_type' => 'billing',
'placeholder' => _x( 'Confirm Email *', 'placeholder', 'woocommerce-email-validation' ),
'required' => true,
'input_class' => array( 'wfacp-form-control' ),
'label_class' => array( 'wfacp-form-control-label' ),
'class' => apply_filters( 'woocommerce_confirm_email_field_class', array( 'wfacp-form-control-wrapper wfacp-col-full' ) ),
'clear' => true,
'validate' => array( 'email' ),
'id' => 'billing_email_2',
);
woocommerce_form_field( 'billing_email_2', $field );
}
public function matching_email_addresses( $data, $errors ) {
$email1 = isset( $_POST['billing_email'] ) ? wc_clean( wp_unslash( $_POST['billing_email'] ) ) : '';
$email2 = isset( $_POST['billing_email_2'] ) ? wc_clean( wp_unslash( $_POST['billing_email_2'] ) ) : '';
if ( empty( $email2 ) ) {
$errors->add( 'validation', $this->email_required_msg );
} elseif ( $email2 !== $email1 ) {
$errors->add( 'validation', $this->email_not_matched );
}
}
public function internal_script() {
if ( false === $this->display_page ) {
return;
}
?>
<script>
(function ($) {
$(document).ready(function () {
var empty = '<?php echo esc_js( $this->email_required_msg ); ?>';
var not_match = '<?php echo esc_js( $this->email_not_matched ); ?>';
wfacp_frontend.hooks.addAction('wfacp_fields_validation', validation_check);
wfacp_frontend.hooks.addFilter('wfacp_field_error_message', error_msg);
if ($('#billing_email').length > 0 && $('#billing_email_2').length > 0) {
$('body').on('focusout', '#billing_email_2', function () {
validation_check();
});
}
function error_msg(msg) {
if (msg == '<strong>Confirm Email Address</strong> is a required field.') {
return empty;
}
if (msg == '<strong>Confirm Email Address</strong> is not a valid email address.') {
return not_match;
}
return msg;
}
function validation_check() {
var billing_email = $('#billing_email').val();
var billing_email_2 = $('#billing_email_2').val();
if ($('#billing_email_field').length > 0 && $('#billing_email_2_field').length > 0) {
if (billing_email === '' || billing_email_2 === '') {
return;
}
if (billing_email_2 !== billing_email) {
$('#billing_email_2_field').addClass('woocommerce-invalid woocommerce-invalid-email woocommerce-invalid-required-field');
} else {
$('#billing_email_2_field').removeClass('woocommerce-invalid woocommerce-invalid-email woocommerce-invalid-required-field');
}
}
}
});
})(jQuery);
</script>
<?php
}
}
new Create_Confirm_Email_Field();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment