There is any way to add a confirm email address field on Wooocommerce registration?
Thanks
There are several ways... Simplest way is to add a new field to registration field and then validate the field when form submitted. Follow this....
Copy wp-content/plugins/woocommerce/templates/myaccount/form-login.php file over to your theme folder wp-content/themes/your-theme/woocommerce/myaccount/form-login.php
Add below code right under *Email address field.. if you have not edited the file yet.. then it's line number 88 where after you have to put it...
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="confirm_reg_email"><?php esc_html_e( 'Confirm Email address', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="email" class="woocommerce-Input woocommerce-Input--text input-text" name="confirm_email" id="confirm_reg_email" autocomplete="email" value="<?php echo ( ! empty( $_POST['confirm_email'] ) ) ? esc_attr( wp_unslash( $_POST['confirm_email'] ) ) : ''; ?>" /><?php // #codingStandardsIgnoreLine ?>
</p>
Add Validation... Check if Email & Confirm Email is same value..
function so_61352749_validate_confirm_email_register_field( $username, $email, $validation_errors ) {
// Confirm email is set but empty
if ( !isset( $_POST['confirm_email'] ) ) {
$validation_errors->add( 'confirm_email_error', __( '<strong>Error</strong>: Confirm Email is required!', 'woocommerce' ) );
}
if ( isset( $_POST['confirm_email'] ) && $_POST['confirm_email'] !== $_POST['email'] ) {
$validation_errors->add( 'confirm_email_error', __( '<strong>Error</strong>: Confirm Email is not matched!', 'woocommerce' ) );
}
return $validation_errors;
}
add_action( 'woocommerce_register_post', 'so_61352749_validate_confirm_email_register_field', 10, 3 );
add_filter( 'woocommerce_checkout_fields' , 'codeithub_add_email_verification_field_checkout' );
function codeithub_add_email_verification_field_checkout( $fields ) {
$fields['billing']['billing_email']['class'] = array( 'form-row-first' );
$fields['billing']['billing_em_ver'] = array(
'label' => 'Confirm mail Address',
'required' => true,
'class' => array( 'form-row-last' ),
'clear' => true,
'priority' => 999,
);
return $fields;
}
add_action('woocommerce_checkout_process', 'codeithub_matching_email_addresses');
function codeithub_matching_email_addresses() {
$email1 = $_POST['billing_email'];
$email2 = $_POST['billing_em_ver'];
if ( $email2 !== $email1 ) {
wc_add_notice( 'Your email addresses do not match', 'error' );
}
}
Related
I've been trying to save the user_description/description meta of authors from woocommerce's /my-account/edit-account.
I'm using the below code to save other meta. but can't figure out to save the user's bio.
// ================= SAVE ACCOUNT FIELDS / START ==================== //
add_action( 'woocommerce_save_account_details', 'misha_save_account_details' );
function misha_save_account_details( $user_id ) {
if( isset( $_POST[ 'billing_phone' ] ) ){
update_user_meta( $user_id, 'billing_phone', wc_clean( $_POST[ 'billing_phone' ] ) );
}
if ( isset( $_FILES['image'] ) ) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
$attachment_id = media_handle_upload( 'image', 0 );
if ( is_wp_error( $attachment_id ) ) {
update_user_meta( $user_id, 'image', $_FILES['image'] . ": " . $attachment_id->get_error_message()[0] );
} else {
update_user_meta( $user_id, 'image', $attachment_id );
}
}
}
// ================= SAVE ACCOUNT FIELDS / END ==================== //
I'm using the below code to display it in there.
// ================= EDIT ACCOUNT FIELDS / START ==================== //
add_action( 'woocommerce_edit_account_form', 'misha_add_field_edit_account_form' );
// or add_action( 'woocommerce_edit_account_form_start', 'misha_add_field_edit_account_form' );
function misha_add_field_edit_account_form() {
// Get current user id
$user_id = get_current_user_id();
woocommerce_form_field(
'billing_phone',
array(
'type' => 'tel',
'required' => true, // remember, this doesn't make the field required, just adds an "*"
'label' => 'Phone number'
),
get_user_meta( $user_id, 'billing_phone', true ) // get the data
);
?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="bio"><?php esc_html_e( 'Bio', 'woocommerce' ); ?> </label>
<textarea class="woocommerce-Input" rows="8" name="user_description"><?= get_the_author_meta( 'user_description', $user_id ); ?></textarea>
</p>
<?php
// Get attachment id
$attachment_id = get_user_meta( $user_id, 'image', true );
// True
if ( $attachment_id ) {
$original_image_url = wp_get_attachment_url( $attachment_id );
echo wp_get_attachment_image( $attachment_id, 'medium');
}else {
echo wp_get_attachment_image(370, 'medium');
}
?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="image"><?php esc_html_e( 'Image', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="file" class="woocommerce-Input" name="image" accept="image/x-png,image/gif,image/jpeg">
</p>
<?php
}
// ================= EDIT ACCOUNT FIELDS / END ==================== //
I've already thoroughly searched this question on google and StackOverflow. Please don't mark it as duplicate or something.
I was wondering why the user_description field is not showing in /edit-account/ by default. So I added it my self woocommerce_edit_account_form but I can't figure out the way to save it.
You can update user description in misha_save_account_details function.
Add this code in misha_save_account_details() function.
if( isset( $_POST[ 'user_description' ] ) ){
update_user_meta( $user_id, 'description', wc_clean( $_POST[ 'user_description' ] ) );
}
Good morning,
I wrote/modified the following code to create 2 different new fileds on woocommerce (bithdate, and city of bith).
Everything works like a charm (in admin page, in my account page) but I'd like to show them on checkout page (so at the first time the customer buy something I can collect that info too [required]).
function action_woocommerce_edit_account_form() {
woocommerce_form_field( 'birthday_field', array(
'type' => 'date',
'label' => __( 'Data di nascita', 'woocommerce' ),
'placeholder' => __( 'Data di nascita', 'woocommerce' ),
'required' => true,
), get_user_meta( get_current_user_id(), 'birthday_field', true ));
woocommerce_form_field( 'birthcity_field', array(
'type' => 'text',
'label' => __( 'Città di nascita', 'woocommerce' ),
'placeholder' => __( 'Città di nascita', 'woocommerce' ),
'required' => true,
), get_user_meta( get_current_user_id(), 'birthcity_field', true ));
}
add_action( 'woocommerce_edit_account_form', 'action_woocommerce_edit_account_form' );
// Validate - my account
function action_woocommerce_save_account_details_errors( $args ){
if ( isset($_POST['birthday_field']) && empty($_POST['birthday_field']) ) {
$args->add( 'error', __( 'Prego inserire data di nascita', 'woocommerce' ) );
}
if ( isset($_POST['birthcity_field']) && empty($_POST['birthcity_field']) ) {
$args->add( 'error', __( 'Prego inserire città di nascita', 'woocommerce' ) );
}
}
add_action( 'woocommerce_save_account_details_errors','action_woocommerce_save_account_details_errors', 10, 1 );
// Save - my account
function action_woocommerce_save_account_details( $user_id ) {
if( isset($_POST['birthday_field']) && ! empty($_POST['birthday_field']) ) {
update_user_meta( $user_id, 'birthday_field', sanitize_text_field($_POST['birthday_field']) );
}
if( isset($_POST['birthcity_field']) && ! empty($_POST['birthcity_field']) ) {
update_user_meta( $user_id, 'birthcity_field', sanitize_text_field($_POST['birthcity_field']) );
}
}
add_action( 'woocommerce_save_account_details', 'action_woocommerce_save_account_details', 10, 1 );
// Add field - admin
function add_user_birtday_field( $user ) {
?>
<h3><?php _e('Dati aggiuntivi','woocommerce' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="birthday_field"><?php _e( 'Data di nascita', 'woocommerce' ); ?></label></th>
<td><input type="date" name="birthday_field" value="<?php echo esc_attr( get_the_author_meta( 'birthday_field', $user->ID )); ?>" class="regular-text" /></td>
</tr>
</table>
<br />
<table class="form-table">
<tr>
<th><label for="birthcity_field"><?php _e( 'Città di nascita', 'woocommerce' ); ?></label></th>
<td><input type="text" name="birthcity_field" value="<?php echo esc_attr( get_the_author_meta( 'birthcity_field', $user->ID )); ?>" class="regular-text" /></td>
</tr>
</table>
<br />
<?php
}
add_action( 'show_user_profile', 'add_user_birtday_field', 10, 1 );
add_action( 'edit_user_profile', 'add_user_birtday_field', 10, 1 );
add_action( 'show_user_profile', 'add_user_birthcity_field', 10, 1 );
add_action( 'edit_user_profile', 'add_user_birthcity_field', 10, 1 );
// Save field - admin
function save_user_birtday_field( $user_id ) {
if( ! empty($_POST['birthday_field']) ) {
update_user_meta( $user_id, 'birthday_field', sanitize_text_field( $_POST['birthday_field'] ) );
}
if( ! empty($_POST['birthcity_field']) ) {
update_user_meta( $user_id, 'birthcity_field', sanitize_text_field( $_POST['birthcity_field'] ) );
}
}
add_action( 'personal_options_update', 'save_user_birtday_field', 10, 1 );
add_action( 'edit_user_profile_update', 'save_user_birtday_field', 10, 1 );
add_action( 'personal_options_update', 'save_user_birthcity_field', 10, 1 );
add_action( 'edit_user_profile_update', 'save_user_birthcity_field', 10, 1 );
I'll be very gratefull to everyone who can help me.
Warm regards
Ale
I solved by myself inspired by this article Post on Stackoverflow, BUT as I usual do I'd like to share the solution because could be helpful for someone.
Based on the code posted above You can add the field on the checkout page with the following code:
// Add the field to the checkout
add_action( 'woocommerce_after_checkout_billing_form', 'birth_day_checkout_field' );
function birth_day_checkout_field( $checkout ) {
// if customer is logged in and his birth date is defined
if(!empty(get_user_meta( get_current_user_id(), 'birthday_field', true ))){
$checkout_birht_date = esc_attr(get_user_meta( get_current_user_id(), 'birthday_field', true ));
}
// The customer is not logged in or his birth date is NOT defined
else {
$checkout_birht_date = $checkout->get_value( 'birthday_field' );
}
echo '<p id="custom_birthday_field">';
woocommerce_form_field( 'birthday_field', array(
'type' => 'date',
'class' => array('birth-date form-row-wide'),
'label' => __( 'Data di nascita', 'woocommerce' ),
'placeholder' => __( 'mm/dd/yyyy', 'woocommerce' ),
'required' => true,
), $checkout_birht_date);
echo '</p>';
}
// Process the checkout
add_action('woocommerce_checkout_process','birth_day_checkout_field_process');
function birth_day_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['birthday_field'] )
wc_add_notice( __( 'Inserire la <strong>Data di nascita</strong>', 'woocommerce' ), 'error' )
;
}
// update order meta with field value
add_action( 'woocommerce_checkout_update_order_meta', 'birth_day_checkout_field_update_order_meta' );
function birth_day_checkout_field_update_order_meta( $order_id ) {
if (!empty( $_POST['birthday_field'])){
update_post_meta( $order_id, 'birthday_field', sanitize_text_field( $_POST['birthday_field'] ) );
// updating user meta (for customer my account edit details page post data)
update_user_meta( get_post_meta( $order_id, '_customer_user', true ), 'birthday_field', sanitize_text_field($_POST['birthday_field']) );
}
}
// update user meta with Birth date (in checkout and my account edit details pages)
add_action ( 'personal_options_update', 'birth_day_save_extra_profile_fields' );
add_action ( 'edit_user_profile_update', 'birth_day_save_extra_profile_fields' );
add_action( 'woocommerce_save_account_details', 'birth_day_save_extra_profile_fields' );
function birth_day_save_extra_profile_fields( $user_id )
{
// for checkout page post data
if ( isset($_POST['birthday_field']) ) {
update_user_meta( $user_id, 'birthday_field', sanitize_text_field($_POST['birthday_field']) );
}
// for customer my account edit details page post data
if ( isset($_POST['birthday_field']) ) {
update_user_meta( $user_id, 'birthday_field', sanitize_text_field($_POST['birthday_field']) );
}
}
In this way this data will be updated everywhere (my account, admin and checkout).
Warm regards
Ale
I have just activated a password confirmation field in my Wordpress registration using this script
function registration_errors_validation($reg_errors, $sanitized_user_login, $user_email) {
global $woocommerce;
extract( $_POST );
if ( strcmp( $password, $password2 ) !== 0 ) {
return new WP_Error( 'registration-error', __( 'Passwords do not match. Please try again.', 'woocommerce' ) );
}
return $reg_errors;
}
add_filter('woocommerce_registration_errors', 'registration_errors_validation', 10,3);
// ----- add a confirm password fields match on the registration page
function wc_register_form_password_repeat() {
?>
<p class="form-row form-row-wide">
<label for="reg_password2"><?php _e( 'Password Repeat', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="password" class="input-text" name="password2" id="reg_password2" value="<?php if ( ! empty( $_POST['password2'] ) ) echo esc_attr( $_POST['password2'] ); ?>" />
</p>
<?php
}
add_action( 'woocommerce_register_form', 'wc_register_form_password_repeat' );
// ----- Validate confirm password field match to the checkout page
function lit_woocommerce_confirm_password_validation( $posted ) {
$checkout = WC()->checkout;
if ( ! is_user_logged_in() && ( $checkout->must_create_account || ! empty( $posted['createaccount'] ) ) ) {
if ( strcmp( $posted['account_password'], $posted['account_confirm_password'] ) !== 0 ) {
wc_add_notice( __( 'Passwords do not match.', 'woocommerce' ), 'error' );
}
}
}
add_action( 'woocommerce_after_checkout_validation', 'lit_woocommerce_confirm_password_validation', 10, 2 );
// ----- Add a confirm password field to the checkout page
function lit_woocommerce_confirm_password_checkout( $checkout ) {
if ( get_option( 'woocommerce_registration_generate_password' ) == 'no' ) {
$fields = $checkout->get_checkout_fields();
$fields['account']['account_confirm_password'] = array(
'type' => 'password',
'label' => __( 'Confirm password', 'woocommerce' ),
'required' => true,
'placeholder' => _x( 'Confirm Password', 'placeholder', 'woocommerce' )
);
$checkout->__set( 'checkout_fields', $fields );
}
}
add_action( 'woocommerce_checkout_init', 'lit_woocommerce_confirm_password_checkout', 10, 1 );
My problem now is that the Error message (triggered by WP_Error) appears on the top of the page after refreshing it.
The behavior I would like to have is to have the error message appearing under the password field.
Any idea on how to achieve it?
I am trying to add a VAT field to the Customer Billing Address, while this works on the Checkout page with the following code:
// Company Name Required
add_filter('woocommerce_checkout_fields','custom_override_checkout_fields');
function custom_override_checkout_fields($fields){
$fields['billing']['billing_company']['required'] = true;
$fields['billing']['billing_vat'] = array(
'label' => __('VAT Number','woocommerce'),
'placeholder' => _x('Enter VAT Number','placeholder','woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}
//Display field value on the order edit page
add_action('woocommerce_admin_order_data_after_shipping_address','my_custom_checkout_field_display_admin_order_meta',10,1);
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('VAT Number').':</strong> ' . get_post_meta($order->id,'_billing_vat',true) . '</p>';
}
//Order the fields
add_filter("woocommerce_checkout_fields","order_fields");
function order_fields($fields){
$order = array(
"billing_first_name",
"billing_last_name",
"billing_company",
"billing_vat",
"billing_country",
"billing_city",
"billing_postcode",
"billing_state",
"billing_address_1",
"billing_address_2",
"billing_email",
"billing_phone",
);
foreach($order as $field){$ordered_fields[$field] = $fields["billing"][$field];}
$fields["billing"] = $ordered_fields;
return $fields;
}
I also require it to be set on the Customer Billing Address in the account options. As I require to link this to the registration page as I'd like the users to register with all their credentials including the VAT number they own for a B2B Webstore.
Does anyone know or could anyone point me in the right direction how I would perform this task of not only showing those billing fields of the VAT number on the checkout page but also on the users profile page, as well as how to add all these fields on the registration page?
Thanks in advance for any assistance for this case!
Well, it's quite simple. Your code should be like this:
/* ---------------------- Registration page ----------------------- */
/* Add extra fields in registration form */
add_action( 'woocommerce_register_form_start', 'my_extra_register_fields' );
function my_extra_register_fields() {
?>
<p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
<label for="reg_billing_vat"><?php _e( 'Billing VAT', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_vat" id="reg_billing_vat" value="<?php if ( ! empty( $_POST['billing_vat'] ) ) esc_attr_e( $_POST['billing_vat'] ); ?>">
</p>
<div class="clearfix"></div>
<?php
}
/* registration form fields Validation */
add_action( 'woocommerce_register_post', 'my_validate_extra_register_fields', 10, 3 );
function my_validate_extra_register_fields( $username, $email, $validation_errors ) {
if ( isset( $_POST['billing_vat'] ) && empty( $_POST['billing_vat'] ) ) {
$validation_errors->add( 'billing_vat_error', __( 'VAT number is required!', 'woocommerce' ) );
}
return $validation_errors;
}
/* Below code save extra fields when new user register */
add_action( 'woocommerce_created_customer', 'my_save_extra_register_fields' );
function my_save_extra_register_fields( $customer_id ) {
if ( isset( $_POST['billing_vat'] ) ) {
// VAT field which is used in WooCommerce
update_user_meta( $customer_id, 'billing_vat', sanitize_text_field( $_POST['billing_first_name'] ) );
}
}
/* ---------------------- Account page ----------------------- */
/* Show custom fields on Account details page */
add_action( 'woocommerce_edit_account_form', 'my_woocommerce_edit_account_form' );
function my_woocommerce_edit_account_form() {
$user_id = get_current_user_id();
$user = get_userdata( $user_id );
if ( !$user ) return;
$billing_vat = get_user_meta( $user_id, 'billing_vat', true );
?>
<fieldset>
<legend>Custom information</legend>
<p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
<label for="billing_vat">Billing VAT</label>
<input type="text" name="billing_vat" id="billing_vat" value="<?php echo esc_attr( $billing_vat ); ?>" class="input-text" />
</p>
<div class="clearfix"></div>
</fieldset>
<?php
}
/* Below code save extra fields when account details page form submitted */
add_action( 'woocommerce_save_account_details', 'my_woocommerce_save_account_details' );
function my_woocommerce_save_account_details( $user_id ) {
if ( isset( $_POST['billing_vat'] ) ) {
update_user_meta( $user_id, 'billing_vat', sanitize_text_field( $_POST['billing_vat'] ) );
}
}
You can add more custom fields as per your need.
And yes you can add custom fields under my-account/edit-address/billing/ by using woocommerce_billing_fields filter hook.
So the code for this, should be like below:
/* Add field under my account billing */
add_filter( 'woocommerce_billing_fields', 'my_woocommerce_billing_fields' );
function my_woocommerce_billing_fields( $fields ) {
$user_id = get_current_user_id();
$user = get_userdata( $user_id );
if ( !$user ) return;
$fields['billing_vat'] = array(
'type' => 'text',
'label' => __('VAT', 'woocommerce'),
'placeholder' => _x('VAT Number', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row'),
'clear' => true,
'default' => get_user_meta( $user_id, 'billing_vat', true ) // assing default value if any
);
return $fields;
}
/* Format custom field to show on my account billing */
add_filter( 'woocommerce_my_account_my_address_formatted_address', 'custom_my_account_my_address_formatted_address', 10, 3 );
function custom_my_account_my_address_formatted_address( $fields, $customer_id, $name ) {
$fields['vat'] = get_user_meta( $customer_id, $name . '_vat', true );
return $fields;
}
/* Replace the key for custom field to show on my account billing */
add_filter( 'woocommerce_formatted_address_replacements', 'custom_formatted_address_replacements', 10, 2 );
function custom_formatted_address_replacements( $address, $args ) {
$address['{vat}'] = '';
if ( ! empty( $args['vat'] ) ) {
$address['{vat}'] = __( 'VAT Number', 'woocommerce' ) . ': ' . $args['vat'];
}
return $address;
}
add_filter( 'woocommerce_localisation_address_formats', 'custom_localisation_address_format' );
function custom_localisation_address_format( $formats ) {
foreach($formats as $key => $value) :
$formats[$key] .= "\n\n{vat}";
endforeach;
return $formats;
}
Am trying to add the code
<?php
// Add the code below to your theme's functions.php file to add a confirm password field on the register form under My Accounts.
add_filter('woocommerce_registration_errors', 'registration_errors_validation', 10,3);
function registration_errors_validation($reg_errors, $sanitized_user_login, $user_email) {
global $woocommerce;
extract( $_POST );
if ( strcmp( $password, $password2 ) !== 0 ) {
return new WP_Error( 'registration-error', __( 'Passwords do not match.', 'woocommerce' ) );
}
return $reg_errors;
}
add_action( 'woocommerce_register_form', 'wc_register_form_password_repeat' );
function wc_register_form_password_repeat() {
?>
<p class="form-row form-row-wide">
<label for="reg_password2"><?php _e( 'Password Repeat', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="password" class="input-text" name="password2" id="reg_password2" value="<?php if ( ! empty( $_POST['password2'] ) ) echo esc_attr( $_POST['password2'] ); ?>" />
</p>
<?php
}
?>
on function.php but not working... Any on tell how to add the password conformation field on woocommerce/myaccout/form-login.php
As a follow up to John's answer:
Hooking to woocommerce_checkout_init no longer works in Woocommerce 3.x, as they introduced a magic getter that will only return the original $checkout->checkout_fields['account'] field, without account2. I don't know if this is a feature or a bug.
For Woocommerce 3.x, the correct way to do it is to use a filter. The validation stays the same.
// Add a second password field to the checkout page in WC 3.x.
add_filter( 'woocommerce_checkout_fields', 'wc_add_confirm_password_checkout', 10, 1 );
function wc_add_confirm_password_checkout( $checkout_fields ) {
if ( get_option( 'woocommerce_registration_generate_password' ) == 'no' ) {
$checkout_fields['account']['account_password2'] = array(
'type' => 'password',
'label' => __( 'Confirm password', 'woocommerce' ),
'required' => true,
'placeholder' => _x( 'Confirm Password', 'placeholder', 'woocommerce' )
);
}
return $checkout_fields;
}
// Check the password and confirm password fields match before allow checkout to proceed.
add_action( 'woocommerce_after_checkout_validation', 'wc_check_confirm_password_matches_checkout', 10, 2 );
function wc_check_confirm_password_matches_checkout( $posted ) {
$checkout = WC()->checkout;
if ( ! is_user_logged_in() && ( $checkout->must_create_account || ! empty( $posted['createaccount'] ) ) ) {
if ( strcmp( $posted['account_password'], $posted['account_password2'] ) !== 0 ) {
wc_add_notice( __( 'Passwords do not match.', 'woocommerce' ), 'error' );
}
}
}
inside your function.php you can do this :
// Add a second password field to the checkout page.
add_action( 'woocommerce_checkout_init', 'wc_add_confirm_password_checkout', 10, 1 );
function wc_add_confirm_password_checkout( $checkout ) {
if ( get_option( 'woocommerce_registration_generate_password' ) == 'no' ) {
$checkout->checkout_fields['account']['account_password2'] = array(
'type' => 'password',
'label' => __( 'Confirm password', 'woocommerce' ),
'required' => true,
'placeholder' => _x( 'Confirm Password', 'placeholder', 'woocommerce' )
);
}
}
// Check the password and confirm password fields match before allow checkout to proceed.
add_action( 'woocommerce_after_checkout_validation', 'wc_check_confirm_password_matches_checkout', 10, 2 );
function wc_check_confirm_password_matches_checkout( $posted ) {
$checkout = WC()->checkout;
if ( ! is_user_logged_in() && ( $checkout->must_create_account || ! empty( $posted['createaccount'] ) ) ) {
if ( strcmp( $posted['account_password'], $posted['account_password2'] ) !== 0 ) {
wc_add_notice( __( 'Passwords do not match.', 'woocommerce' ), 'error' );
}
}
}
<?php
// Add the code below to your theme's functions.php file to add a confirm password field on the register form under My Accounts.
add_filter('woocommerce_registration_errors', 'registration_errors_validation', 10,3);
function registration_errors_validation($reg_errors, $sanitized_user_login, $user_email) {
global $woocommerce;
extract( $_POST );
if ( strcmp( $password, $password2 ) !== 0 ) {
return new WP_Error( 'registration-error', __( 'Passwords do not match.', 'woocommerce' ) );
}
return $reg_errors;
}
add_action( 'woocommerce_register_form', 'wc_register_form_password_repeat' );
function wc_register_form_password_repeat() {
?>
<p class="form-row form-row-wide">
<label for="reg_password2"><?php _e( 'Password Repeat', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="password" class="input-text" name="password2" id="reg_password2" value="<?php if ( ! empty( $_POST['password2'] ) ) echo esc_attr( $_POST['password2'] ); ?>" />
</p>
<?php
}
?>
source : you can find this code here i test before on my website
here Two Method one for Checkout and second for My Account below i mentioned both code
add_action( 'woocommerce_checkout_init', 'wc_add_confirm_password_checkout', 10, 1 );
function wc_add_confirm_password_checkout( $checkout ) {
if ( get_option( 'woocommerce_registration_generate_password' ) == 'no' ) {
$checkout->checkout_fields['account']['account_password2'] = array(
'type' => 'password',
'label' => __( 'Confirm password', 'woocommerce' ),
'required' => true,
'placeholder' => _x( 'Confirm Password', 'placeholder', 'woocommerce' )
);
}
}
// Check the password and confirm password fields match before allow checkout to proceed.
add_action( 'woocommerce_after_checkout_validation', 'wc_check_confirm_password_matches_checkout', 10, 2 );
function wc_check_confirm_password_matches_checkout( $posted ) {
$checkout = WC()->checkout;
if ( ! is_user_logged_in() && ( $checkout->must_create_account || ! empty( $posted['createaccount'] ) ) ) {
if ( strcmp( $posted['account_password'], $posted['account_password2'] ) !== 0 ) {
wc_add_notice( __( 'Passwords do not match.', 'woocommerce' ), 'error' );
}
}
}
add_filter('woocommerce_registration_errors', 'registration_errors_validation', 10,3);
function registration_errors_validation($reg_errors, $sanitized_user_login, $user_email) {
global $woocommerce;
extract( $_POST );
if ( strcmp( $password, $password2 ) !== 0 ) {
return new WP_Error( 'registration-error', __( 'Passwords do not match.', 'woocommerce' ) );
}
return $reg_errors;
}
#
My Account Page
#
add_action( 'woocommerce_register_form', 'wc_register_form_password_repeat' );
function wc_register_form_password_repeat() {
?>
<p class="form-row form-row-wide">
<label for="reg_password2"><?php _e( 'Password Repeat', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="password" class="input-text" name="password2" id="reg_password2" value="<?php if ( ! empty( $_POST['password2'] ) ) echo esc_attr( $_POST['password2'] ); ?>" />
</p>
<?php
}
After so much effort, finally worked the following code with revised a little bit as per my requirement to display Confirm password in my account page. Please try the following code in functions.php
// Add the code below to your theme's functions.php file to add a confirm password field on the register form under My Accounts.
add_filter('woocommerce_registration_errors', 'registration_errors_validation', 10,3);
function registration_errors_validation($reg_errors, $sanitized_user_login, $user_email) {
global $woocommerce;
extract( $_POST );
if ( strcmp( $password, $password2 ) !== 0 ) {
return new WP_Error( 'registration-error', __( 'Passwords do not match.', 'woocommerce' ) );
}
return $reg_errors;
}
add_action( 'woocommerce_register_form', 'wc_register_form_password_repeat' );
function wc_register_form_password_repeat() {
?>
<div class="form-row form-row-wide addtafterpassword" style=" margin-top: 20px; margin-bottom: 0;">
<label for="reg_password2"><?php _e( 'Password Repeat', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="password" class="input-text" name="password2" id="reg_password2" value="<?php if ( ! empty( $_POST['password2'] ) ) echo esc_attr( $_POST['password2'] ); ?>" />
</div>
<?php
}
?>