skip to Main Content

I’m working on a development and I need that users already registered on my site, change their password obligatorily on the woocommerce checkout-page.

I tried the classic solution:

add_filter('woocommerce_checkout_fields', 'custom_override_checkout_fields');

function custom_override_checkout_fields($fields)
{
    $fields['account']['account_password'] = array(
      'type' => 'password',
      'required'  => true,
      'label' => __('Pass', 'woocommerce'),
      'placeholder' => _x('Pass', 'placeholder', 'woocommerce'),
      'class' => array('form-row-wide')
  );

    return $fields;
}

This mode only works for me when the user is registering, but not for changing their password.

Do you think I should make a wp_set_password( $password, $user_id ); ? If so, where would you place the code?

I need a way to do an UPDATE and change user_pass from wp_users table.

2

Answers


  1. Chosen as BEST ANSWER

    I tried with a mix between @Raihan code and Googling´s code but I can't change user_pass. With this code it dont show "Internal server error" by Woocommerce.

    add_action( 'woocommerce_checkout_process', 'user_fields_woocommerce_checkout_process' );
    
    function user_fields_woocommerce_checkout_process(){
        if( is_user_logged_in() ){
            add_action('woocommerce_checkout_update_user_meta',
            'my_custom_checkout_field_update_user_meta' );
        }
    }
    
    function my_custom_checkout_field_update_user_meta( $user_id ) {
        if ( $data['account_password'] ) {
            update_user_meta( $user_id, 'account_password', $data['account_password'] );
        }
    }
    

  2. Try it.

    add_action( 'woocommerce_checkout_update_user_meta', 'wc_checkout_field_update_user_meta' );
    
       function wc_checkout_field_update_user_meta( $user_id, $data ) { 
    
         if ( $data['account_password'] ) {   
    
           update_user_meta( $user_id, 'account_password', $data['account_password'] );
         }
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search