Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 997
kris Avatar asked Apr 03 '17 07:04

kris


People also ask

How to edit order in WooCommerce using edit order by customer?

After activation of the Edit Order by Customer extension, the “ Edit Order by Customer ” menu will be displayed under WP Admin > WooCommerce. Click to view settings. Within the General Settings tab, the following options are available: Enable edit order feature for specific user roles and customers

How to add new user roles in WooCommerce?

User Registration for WooCommerce allows the functionality to add new user roles. To add a new user role, navigate to WordPress Admin Dashboard > Users > User Roles. Enter the Name, Slug, and Description of the user role. Once done, click Add New Role. Once the user role is successfully added, it will appear on the right side of the page.

How do I add a customer management plugin to WooCommerce?

Vendre des produits Allow admins and store managers to act on behalf of customers in your WooCommerce store. Download the .zip file from your WooCommerce account. Go to : WordPress Admin > Plugins > Add New and Upload Plugin the file you have downloaded. Install Now and Activate.

What is user registration for WooCommerce?

User Registration for WooCommerce allows you to automate your wholesale registration system for B2B and wholesale customers with the use of a registration form builder making user approval simple and easy. Download the User Registration for WooCommerce .zip file from your WooCommerce account.


2 Answers

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());
like image 103
Vidish Purohit Avatar answered Oct 13 '22 01:10

Vidish Purohit


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!

like image 32
Shameem Ali Avatar answered Oct 13 '22 00:10

Shameem Ali