dsqe46004 2018-09-20 16:15
浏览 136
已采纳

在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?

  • 写回答

2条回答 默认 最新

  • dshgdhdfcas30210 2018-09-21 00:27
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 全志H618ROM新增分区
  • ¥20 jupyter保存图像功能的实现
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况