Programmatically Create A WooCommerce Order

About The Author
Cabe Nolan is the founder of WP Cover where he shares his insight into WordPress, development, & entrepreneurship. Outside of WPCover, Cabe continues to run a successful WordPress development firm, Bold City Design as well as a few high profile websites, Arrivala, Two Way Resume, Dock Skipper, and a successful outdoors brand, DolfinPack.

Programmatically Create A WooCommerce Order

Today we’re going to dive into programmatically creating a WooCommerce order through a form submission.  Quick background, we had a client that needed a custom funnel created that was a bit outside of what WooCommerce would have allowed.  However, they were using a product fulfillment center that was planning to connect to WooCommerce via a pre-made plugin to download and fulfill/ship the orders.  Therefore, we needed to create a custom page that housed the funnel process but then needed to hook back into WooCommerce to create the order.

What did we do?

We started with a basic form and collected the information we needed to collect.  During the form submission we proceeded to create the order by hooking into pre-existing WooCommerce functions.  Here’s some sample code that we’ll go through:

global $woocommerce;
// here we are creating the users address.  All of these variables have been assigned data previously in the submission process
$address = array(
	'first_name' => $firstname,
	'last_name'  => $lastname,
	'company'    => '',
	'email'      => $youremail,
	'phone'      => $yourphone,
	'address_1'  => $youraddress,
	'address_2'  => '',
	'city'       => $yourcity,
	'state'      => $yourstate,
	'postcode'   => $yourzip,
	'country'    => 'US'
// first we create the order.  We already have assigned $user_id the user ID of the customer placing the order.
$order = wc_create_order(array('customer_id' => $user_id));

// The add_product() function below is located in /plugins/woocommerce/includes/abstracts/abstract_wc_order.php
$order->add_product( get_product( '782' ), 1 ); // This is the ID of an existing SIMPLE product
// You can add more products if needed by repeating the above line
$order->set_address( $address, 'billing' );
$order->set_address( $address, 'shipping' );

$shipping_tax = array(); 
// Here we're going to assign a custom shipping method.
$shipping_rate = new WC_Shipping_Rate( '', 'Flat Rate', '5.95', $shipping_tax, 'custom_shipping_method' );

// here we are adding some system notes to the order
$order->update_status("processing", 'Imported Order From Funnel', TRUE);


And that’s it! Please refer to the comments within the code to get a better understanding.


  1. Giang says:


    I tried this but did not get email inform new orders…

    Is there any way we can trigger sending mail as order created?


Share Your Thoughts

Leave a Reply

Your email address will not be published.

Related Stuff You Might Like

WordPress News, Tips, & Code Snippets

Join the WP Cover mailing list and get wordpress news, tips, code snippets, security warnings, and more delivered right to your inbox.  We won't flood your inbox, newsletters typically go out every 1-2 weeks unless it involves an important security release.

You have Successfully Subscribed!