0

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' );
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Rob
  • 6,304
  • 24
  • 83
  • 189
  • Doesn't `$meta` and `$oldmeta` contain all the information like Company, Billing address's etc? – IsThisJavascript Aug 16 '19 at 10:35
  • Maybe this answer to this question may be able to help you out a bit, https://stackoverflow.com/questions/22843504/how-to-get-customer-details-from-order-in-woocommerce – IsThisJavascript Aug 16 '19 at 10:36
  • @IsThisJavascript It's not quite related to the order because they can go into their account and change their details without ordering anything new. I guess it's more of a user meta question??? – Rob Aug 16 '19 at 10:40

2 Answers2

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.

itzmekhokan
  • 2,597
  • 2
  • 9
  • 9
  • Ah ok, so could I do something like `add_action( 'woocommerce_customer_save_address', 'update_meta_info', 10, 2 ); function update_meta_info( $user_id, $load_address ) { //do something }`? – Rob Aug 16 '19 at 12:08
  • Exactly, and do a checking within your function that `if( $load_action != 'billing' ) return;` – itzmekhokan Aug 16 '19 at 12:16
  • Ok great, one thing I don't understand is how I break down each line of the address using `$load_address`? – Rob Aug 16 '19 at 12:26
  • You can get all the submitted address data within your function, if you print `$_POST` array – itzmekhokan Aug 16 '19 at 12:36
  • I've updated my question with what I want to do... I want to put the address into the data array. Is that possible? – Rob Aug 16 '19 at 13:26
  • Then you can build a array from `$_POST` data with all billing information and replace in data or just simply replace `'data' => $_POST` to shore all submitted data all together. – itzmekhokan Aug 17 '19 at 06:56
0

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.

Teowin
  • 1