Search code examples
phpwordpresswoocommercehook-woocommerceorders

How to get the order information inside the customer creation hook?


When a visitor buys a new product in my WooCommerce shop, I'm using the woocommerce_created_customer hook to create a new user on an external database with some information I get from the $customer_id, $new_customer_data, $password_generated arguments.

For example:

function action_woocommerce_created_customer($customer_id, $new_customer_data, $password_generated) {
    // Create a new user on external database
}
add_action('woocommerce_created_customer', 'action_woocommerce_created_customer', 10, 3);

Well. What I need is a connection to the current order to get the order id. In my shop, a user account can only be created in combination with an order. Is there a way to get order information inside the woocommerce_created_customer hook? Or is this do_action just called before the order is done?

I thought about using another hook after the payment is done. But this is not possible in my case because this is the only hook I found which I can get the unhashed user password, which I very need.

Do you have an idea how to get the order information inside the customer creation hook?


Solution

  • The order is created after the customer.

    $this->process_customer( $posted_data );
    $order_id = $this->create_order( $posted_data );
    $order    = wc_get_order( $order_id );
    

    Use this hook to do the stuff.

    do_action( 'woocommerce_checkout_order_processed', $order_id, $posted_data, $order );
    

    $posted_data contains the username and password

    $username    = ! empty( $posted_data['account_username'] ) ? $posted_data['account_username'] : '';
    $password    = ! empty( $posted_data['account_password'] ) ? $posted_data['account_password'] : '';