How to Create Order Programmatically WooCommerce

WooCommerce Create Order ProgrammaticallyWhen you are creating a WooCommerce plugin that creates and manages WooCommerce orders, you may want to  create order programmatically.

If this is something you have in mind and want to see an example of how you can get this done, I want to share with you a good example of a function I have used before in my plugins to create order programmatically in WooCommerce. I also shared in a previous post how to  Change  WooCommerce product price programmatically

WooCommerce Create Order Programmatically

We can use a function to create the order programmatically that sets the order number first, loops through the orders and sets the address for the automatic order.

We can then use the wc_create_order() function to create order programmatically and set the products in the order and the order status to complete for the order to be completed.

I have created an example function below with all the necessary code that you can repurpose to create order programmatically in WooCommerce.

Ideally, you should add this code to the functions.php or your plugin files and then call the function where to need it. You can also adjust the $number_of_orders variable to change the number of orders that are created when the function is called.

The following is the code that you can use to create automatic orders in WooCommerce :

<?php

function create_Orders_Programmatically(){

	// Set numbers of orders to create
	$number_of_orders = 50;

	for ($i=0; $i < $number_of_orders; $i++) { global $woocommerce; // Get a random number of products, between 1 and 10, for the order $products = get_posts( array( 'post_type' => 'product',
			'posts_per_page' => rand(1,10),
			'orderby' => 'rand'
		) );

		// Set order address
		$address = array(
			'first_name' => 'Joe ' . rand(1,200),
			'last_name'  => 'Njenga',
			'company'    => 'njengah.com',
			'email'      => 'joe@example.com',
			'phone'      => '894-672-780',
			'address_1'  => '123 Main st.',
			'address_2'  => '100',
			'city'       => 'Nairobi',
			'state'      => 'Nairobi',
			'postcode'   => '00100',
			'country'    => 'KE'
		);

		// Now we create the order
		$order = wc_create_order();

		// Add products randomly selected above to the order
		foreach ($products as $product) {
			$order->add_product( wc_get_product($product->ID), 1); // This is an existing SIMPLE product
		}

		$order->set_address( $address, 'billing' );

		$order->calculate_totals();
		$order->update_status("Completed", 'Imported order', TRUE); 
	}
	

As you can see in the code above, its possible to create programmatic orders using the steps I highlighted above. You can adjust the variable in each case to suit your needs.

In the case of this example I am creating 50 orders and this logic can be extended further to add more specific products that can have automatic orders.

Conclusion

In this post, I have highlighted how you can create WooCommerce orders programmatically using a function that sets the order variable, sets address and creates the WooCommerce order.  I hope this code helps you create the order programmatically that suits your needs. If you need further help in getting this code to work, you should get in touch with me.

Similar Articles