Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

351
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda