简体   繁体   中英

Create user on order status completed action hook in WooCommerce 3+

I am trying to create a new user within the woocommerce_order_status_completed hook.

add_action( 'woocommerce_order_status_completed', 'custom_woocommerce_order_status_completed', 10, 1 );

function custom_woocommerce_order_status_completed( $order_id )
{
    $password = wp_generate_password( 16, true );
    $user_id = wp_create_user( 'usertest@gmail.com', $password, 'usertest@gmail.com' );
}

$user_id returns an actual ID. It appears to create the user but when i look at the backend the user is not there. I even check the database for the user id and it's not there.

If i call the same function on the action woocommerce_after_register_post_type it creates the user.

Any idea what could be causing this issue?

您的函数名称似乎与您的钩子中的一个调用不匹配:

custom_woocommerce_order_status_completed

The following code uses Woocommerce wc_create_new_customer() dedicated function instead:

add_action( 'woocommerce_order_status_completed', 'action_on_order_status_completed', 20, 2 );
function action_on_order_status_completed( $order_id, $order ){
    $password    = wp_generate_password( 16, true );
    $user_name   = $user_email = 'usertest@gmail.com';
    // $user_name   = $user_email = $order->get_billing_email();
    $customer_id = wc_create_new_customer( $user_email, $user_name, $password );
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM