Programmatically Create A WooCommerce Order

Aug 14, 2017

Programmatically Create A WooCommerce Order


This post was originally created on our sister site WP Cover.
View Original Article
August 14, 2017

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' );
$order->add_shipping($shipping_rate);

$order->calculate_totals();
// 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.

The post Programmatically Create A WooCommerce Order appeared first on WP Cover.

Additional News

Perfect!

Let's get started. Fill out the form below to email us or request a call back.


FREE: 10 Step Guide To Choosing A Web Design Company

FREE: 10 Step Guide To Choosing A Web Design Company

Download your free e-book. Learn the questions you should be asking prior to partnering with a web design company. Enter your email address and we'll send it to you right away!

We've sent the e-book to your email address!