Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce codex : How to create an order from an existing cart (wc_create_order)

I am creating a plugin that handles the full checkout for a WooCommerce cart.
It will do the payment and shipping and I simply need to then programatically create the order correctly in Woo Commerce.

I know I want to use wc_create_order(),
is there something I can then do to fill the order details from the current WC()->cart info?

And, as a 2nd part to this question, I will have a shipping address, how do I add that to the newly created order?

like image 565
kris Avatar asked Dec 24 '22 19:12

kris


1 Answers

Here is what I ended up with:

$cart = WC()->cart;
$checkout = WC()->checkout();
$order_id = $checkout->create_order(array());
$order = wc_get_order($order_id);
update_post_meta($order_id, '_customer_user', get_current_user_id());
$order->calculate_totals();
$order->payment_complete(); 
$cart->empty_cart();

This question helped with the setting the user id part.

like image 99
kris Avatar answered Jan 14 '23 13:01

kris