WooCommerce 中 wp_set_password 函数的等价物是什么?

What is the equivalent of wp_set_password function in WooCommerce?

我发现我可以自定义 wp_set_password 函数并将我的代码放入其中。但是只有当用户通过 /wp-login.php.

注册时才会执行

这是我的代码:

function wp_set_password( $password, $user_id ) {
    // Keep original WP code
    global $wpdb;

    $hash = wp_hash_password( $password );
    $wpdb->update(
        $wpdb->users,
        array(
            'user_pass'           => $hash,
            'user_activation_key' => '',
        ),
        array( 'ID' => $user_id )
    );

    wp_cache_delete( $user_id, 'users' );

    // and now add your own
    $custom_hash = password_hash( $password, PASSWORD_DEFAULT );
    update_user_meta($user_id, 'user_pass2', $custom_hash);
}

但是我已经安装了 WooCommerce,关于 密码 的所有三个主要任务是:

所以这段代码对我没有帮助,我在 WooCommerce 中搜索了类似的功能,但我找不到。无论如何,我可以在我的自定义插件中像这样编辑 WooCommerce 吗?这样做的功能是什么?

You should always avoid to overwrite any core file, as you will loose your changes when WordPress will be updated and you can make big trouble in this related sensible processes.

在 Woocommerce 中相当于 WordPress wp_set_password() is the WC_Customer set_password() 方法。

要使其可插入,您可以使用 [WC_Customer_Data_Store][4] Class 位于 update() 方法中的相关钩子

  • 在创建用户时,对于 "User registration",使用 woocommerce_new_customer 操作挂钩。
  • 在用户更新事件中,使用 woocommerce_update_customer 操作挂钩
  • 在"User registration"(用户创建)上,你可以使用woocommerce_new_customer动作挂钩
  • 当 changing/saving 我的帐户 > 帐户详细信息部分的密码时,您也可以使用 woocommerce_save_account_details 操作挂钩。
  • 重设密码后,您可以使用 woocommerce_customer_reset_passwor 操作挂钩。

WC_Customer set_password() 方法的用法示例:

// Get the WC_Customer instance object from the user ID
$customer = new WC_Customer( $user_id );

// Set password
$customer->set_password( $password );

// Save to database and sync
$customer->save();