WooCommerce codex : how to set the user/customer on an order

enter image description here

I am writing a WooCommerce Plugin that takes care of payment and delivery.
Right now I am at the point of creating an order based on the current shopping cart.
That all works fine getting the items and costs correct, the only problem is that the order shows as being made by “Guest”, instead of by the currently logged on user (even though the correct email address for that user is on the order).

Here is my code :

    $cart = WC()->cart;
    $checkout = WC()->checkout();
    $order_id = $checkout->create_order();
    $order = wc_get_order( $order_id );
    $order->user_id = apply_filters( 'woocommerce_checkout_customer_id', get_current_user_id() );
    $order->calculate_totals();
    $order->payment_complete(); 
    $cart->empty_cart();

Here is what I see in the backend after running this :

Why is the order placed as “Guest” instead of the user that placed the order?
And how do I get to have the correct user attached?

Read more here: WooCommerce codex : how to set the user/customer on an order

Leave a Reply

Your email address will not be published. Required fields are marked *