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?
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
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.
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.
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.
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());
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With