Skip to content

Instantly share code, notes, and snippets.

@xlplugins
Last active October 26, 2023 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xlplugins/f7671c9bef62411a4b4c1fc91bf403b0 to your computer and use it in GitHub Desktop.
Save xlplugins/f7671c9bef62411a4b4c1fc91bf403b0 to your computer and use it in GitHub Desktop.
Create confirm Email field after email field
class Create_Confirm_Email_Field {
public $email_required_msg = '';
public $email_not_matched = '';
public function __construct() {
add_action( 'wfacp_after_billing_email_field', [ $this, 'create_field' ] );
add_action( 'wfacp_template_load', [ $this, 'action' ] );
/* validation for first step */
add_action( 'wfacp_internal_css', [ $this, 'internal_script' ] );
}
public function action() {
$this->email_required_msg = __( '<b>Confirm E-Mails</b> is a required field.', 'woocommerce' );
$this->email_not_matched = __( '<b>Confirm E-Mails</b> do not match.', 'woocommerce' );
add_action( 'woocommerce_after_checkout_validation', [ $this, 'matching_email_addresses' ], 8, 2 );
}
public function create_field() {
$this->field = array(
'label' => __( 'Confirm Email Address', 'woocommerce-email-validation' ),
'type' => 'email',
'field_type' => 'billing',
'placeholder' => _x( 'Email Address', 'placeholder', 'woocommerce-email-validation' ),
'required' => true,
'input_class' => [ 'wfacp-form-control' ],
'label_class' => [ 'wfacp-form-control-label' ],
'class' => apply_filters( 'woocommerce_confirm_email_field_class', [ 'wfacp-form-control-wrapper wfacp-col-full ' ] ),
'clear' => true,
'validate' => array( 'email' ),
'id' => 'billing_email_2',
);
woocommerce_form_field( 'billing_email_2', $this->field );
}
public function matching_email_addresses( $data, $errors ) {
$email1 = $_POST['billing_email'];
$email2 = $_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() {
?>
<script>
(function ($) {
$(document).ready(function () {
var empty = '<?php echo $this->email_required_msg; ?>';
var not_match = '<?php echo $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