admin管理员组

文章数量:1122832

On my website I have created a register page in react and I am using Wordpress headless with WooCommerce on it.

So I was expecting to create a new user using the register page i have and as the Wordpress side is by default set to create a 'Customer" user type for any new registration.

The registration is partially working. Let me explaining. To achieve the registration I have done this:

  • On wordpress, I have installed the plugin : WP Rest user from Sk8tech
  • I have called the api /wp-json/wp/v2/users/register with a body which contain the username, email and passwork.
  • The new user is created and have the "Customer" type

However this new user do not shows up in the WooCommerce Customer Dashboard.

When I have installed the WooCommerce plugin, at first, I have change the type of user manually through the Wordpress dashboard from subscriber to customer and they all shows up in the Customer part.

I have notice that WooCommerce do have an api to create a new customer /wp-json/wc/v3/customers but I cannot setup a password.

So Do I have to make 2 calls and the WooCommerce one, will automaticall create a new customer ?

Regards

On my website I have created a register page in react and I am using Wordpress headless with WooCommerce on it.

So I was expecting to create a new user using the register page i have and as the Wordpress side is by default set to create a 'Customer" user type for any new registration.

The registration is partially working. Let me explaining. To achieve the registration I have done this:

  • On wordpress, I have installed the plugin : WP Rest user from Sk8tech
  • I have called the api /wp-json/wp/v2/users/register with a body which contain the username, email and passwork.
  • The new user is created and have the "Customer" type

However this new user do not shows up in the WooCommerce Customer Dashboard.

When I have installed the WooCommerce plugin, at first, I have change the type of user manually through the Wordpress dashboard from subscriber to customer and they all shows up in the Customer part.

I have notice that WooCommerce do have an api to create a new customer /wp-json/wc/v3/customers but I cannot setup a password.

So Do I have to make 2 calls and the WooCommerce one, will automaticall create a new customer ?

Regards

Share Improve this question asked Aug 31, 2020 at 6:15 SebSeb 1091 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

In the documentation of WP REST User https://wordpress.org/plugins/wp-rest-user/ you have the aswer:

To perform further actions after user is registered, write and add_action:

add_action('wp_rest_user_user_register', 'user_registered');
function user_registered($user) {
    // Do Something
}

Replace "// Do Something" for the code to change the user role. Something like this (not tested):

$u = new WP_User( 3 ); //set your user ID
// Remove role
$u->remove_role( 'subscriber' );
// Add role
$u->add_role( 'customer' );

本文标签: wp adminImpossible to create a new WooCommerce customer using Rest API