Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

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

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 :

enter image description here


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?

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You will need to add _customer_user to post_meta table with order ID and meta value should be the user ID which you want to link to this order.

I hope that this will help you:

update_post_meta($order_id, '_customer_user', get_current_user_id());
about 4 years ago · Santiago Trujillo Denunciar

0

OR

use user/customer id(create a customer/user before place order) in wc_create_order();

eg: $order = wc_create_order(array('customer_id' => $user));

this code fine for me!

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda