0

I want to update the user meta when customer changes their first name.

here my code:

add_action( 'woocommerce_update_customer', 'can_editable_account_count' ); 
function can_editable_account_count( $customer_get_id ) {
    if ( ! is_account_page() && ! is_user_logged_in() ) {
       return;
    }
    
    update_user_meta($customer_get_id, 'can_editable_account', '0');
}

but is run whatever change customer made. I want only run if the specific field was changed.

1 Answer 1

0

That was a tricky one.

For the user meta update to happen only on first name update you will need the following.

  1. Create a constant that will contain the current user first name
  2. Check to see if the current user first name is different from the submitted user name on profile update

For the constant add the following line. I would suggest at the top of the function.php

define('USER_FIRST_NAME', get_user_meta(get_current_user_id(), 'first_name', true));

Now for the function

function can_editable_account_count( $customer_get_id ) {
    if ( ! is_account_page() && ! is_user_logged_in() ) {
        return;
    }

    // check if user submited the edit account form and if the first name field exists and has value
    if (isset($_POST['account_first_name']) && !empty($_POST['account_first_name'])) {
        // check if the current user name is not equal to the submited user name
        if ($_POST['account_first_name'] !== USER_FIRST_NAME) {
            update_user_meta($customer_get_id, 'can_editable_account', '0');
        }
    }
}
4
  • You probably ought not call get_current_user_id until the plugins_loaded event in case you have a plugin that overrides one of the pluggable functions it uses, or registers another determine_current_user hook handler that your site uses instead. But chances are that won't matter on the average WooCommerce site.
    – Rup
    Commented Feb 2, 2021 at 9:44
  • However I'd hope there'd be a better way to do this, e.g. a pre-edit hook where you can capture the current values when you need them. But I don't know of one.
    – Rup
    Commented Feb 2, 2021 at 9:45
  • get_current_user_id() is a wordpress function and if the user is not logged in it will return 0. its safe to use it. the constant will only be used when the user is updating the profile so it will always have a valid value. I agree that this method is sort of a hack and a action right before profile update will be much better =], I could not find one so this is the best I could do under the circumstances. Commented Feb 2, 2021 at 9:50
  • It will work, yes, but you can hook into it and override its behaviour. If you're calling it from the top of a plugin functions.php then you're potentially calling it before everyone else has had a chance to hook into it and so if you have site-specific code or other plugin code that helps determine the current user then that won't be run and it'll only determine the current user from WordPress's cookies. So whilst it should be OK, it isn't completely safe to do in the general case. But if you meant the theme's functions.php that should be safe though.
    – Rup
    Commented Feb 2, 2021 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.