Rob
Rob

Reputation: 6370

Detect when a user changes any of their billing details (user meta)

I have a Woocommerce site that I want to detect if a user changes any of their billing details. I've had a look around and found this snippet:

add_filter( 'insert_user_meta', function( $meta, $user, $update ) {
  if( true !== $update ) return $meta;
  $old_meta = get_user_meta( $user->ID );
  if( $old_meta[ 'first_name' ] !== $meta[ 'first_name' ] ) {
    //* Do something
  }
  return $meta;
}, 10, 3 );

Will this fire everytime an update is made to the user meta? How can I add in all the following fields to see if any change:

Or alternatively, is there a woocommerce hook for this?

Would something like this work?

function my_profile_update( $user_id ) {
    if ( ! isset( $_POST['pass1'] ) || '' == $_POST['pass1'] ) {
        return;
    }

    // password changed...
}
add_action( 'profile_update', 'my_profile_update' );

Upvotes: 0

Views: 666

Answers (2)

itzmekhokan
itzmekhokan

Reputation: 2770

If a user edit address from woocommerce my account page in frontend, there is a hook available for edit address through which you can check and achieve your work. The hook as follow -

do_action( 'woocommerce_customer_save_address', $user_id, $load_address );

where $load_address return 'billing' whenever a user edit billing fields.

Upvotes: 2

Teowin
Teowin

Reputation: 1

I would say that if the hook works, it is triggered for all Wordpress users, not only customers.

But I would recommend not to compare on data that a user can enter. In your case if he made a typo in his first name (like uppercase first letter), comparision fails and one can never correct that.

What you are looking for is one of the account page hooks. But unfortunately I can only point out a direction.

Upvotes: 0

Related Questions