skip to Main Content

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:

  • Company
  • Billing address line 1
  • Billing address line 2
  • City
  • Postcode
  • Country
  • County

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' );

2

Answers


  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.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search