WooCommerce: Create An Order From Another Website!

This is going to be a great tutorial. And it works for sure, because I’ve just implemented it on the brand new WooWeekly website!

Basically I was looking for a way to create an order on Business Bloomer WooCommerce website when a customer registered from the My Account page on the WooWeekly WooCommerce website. The reason for doing that is that I’m using email marketing on Business Bloomer, and the only way to add an email contact from another website was by using the “REST API” that WooCommerce provides.

Now, I learned all this today, so you can manage to achieve complex stuff too. I’ll just save you a couple of hours of headaches trying to figure out how the system works – that’s why you’re here!

So, how do you create a WooCommerce order on one website when an event occurs on another website? Enjoy!

1. Create a set of REST API keys

Go to the website where you want the order to be created, then go to WooCommerce -> Settings -> Advanced -> REST API -> Add key.

Enter your description, and most importantly set the permissions to “Read/Write”. Click on “Generate API key”:

You will now be presented with a set of keys, and specifically a “consumer key” and a “consumer secret”:

Now, your website where you wish to create orders (or anything e.g. WooCommerce products, customers etc.) is “ready” as we just created a way for another website to communicate with it, read and write.

2. Add code to the “other” website

As soon as something happens on the other website e.g. a form is submitted, you can use some code to “send the data” to the website where you want to create the order programmatically.

In my case I’ve selected the “woocommerce_created_customer” hook: as soon as a WooCommerce customer registers on https://wcwkly.com (see the registration form on top of the homepage?) I want an order created on Business Bloomer. I have created API keys on Business Bloomer, so I can connect the two and let them work their magic.

PHP Snippet: Create Order On WooCommerce Website “A” When An Event Occurs On Website “B”

Note: of course paste your consumer key and secret key codes into $live_ck and $live_cs variables. Also, change $live_url website address to yours!

/**
 * @snippet       Create WooCommerce Order via API
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 5
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_action( 'woocommerce_created_customer', 'bbloomer_create_order_from_wcwkly', 9999, 3 ); 
	
function bbloomer_create_order_from_wcwkly( $customer_id, $new_customer_data, $password_generated ) {
	$live_ck = 'ck_blablabla';
	$live_cs = 'cs_blablabla';
	$live_url = 'https://www.businessbloomer.com/wp-json/wc/v3/orders?consumer_key=' . $live_ck . '&consumer_secret=' . $live_cs;
	$customer = new WC_Customer( $customer_id );
	$body = array(
		'status' => 'completed',
		'meta_data' => array( array( 
			'key' => 'createdby',
			'value' => 'wcwkly.com' 
		)),
		'total' => 0,
		'billing' => array(
			'first_name' => $customer->get_billing_first_name(),
			'email' => $customer->get_email(),
		),
		'line_items' => array( array( 
			'product_id' => 195376,
			'quantity' => 1,
		)),
	);
	$raw_response = wp_remote_post( $live_url, 
		array(
			'headers' => array( 'Content-Type' => 'application/json' ),
			'timeout' => 30,                    
			'body' => json_encode( $body ),
		)
	);
}

In my case, as you can see from the code, I’m creating an order with status completed, I’m logging some meta data in it by saying that the order was created by the other website URL, I’m setting the total to $0, I’m adding billing first name and email, and a product with ID = 195376 and quantity = 1. All parameters that can be used are here: https://woocommerce.github.io/woocommerce-rest-api-docs/#order-properties – you can set any parameter that is not “read-only”.

Final result? There you go. Test user entered “www” as first name and “rodolfomelogli……@….” as billing email which is the dynamic bit of data, while the rest was set through code.

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (delete "?>" if you have it there). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.3.

If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

8 thoughts on “WooCommerce: Create An Order From Another Website!

  1. Hi sir,

    I am wondering,

    if customer/user has purchased a product on website A.

    Can we get the the status on another website B, using REST API.

    Also, if purchased show hidden content of website B based on REST API response.

    1. Hi Arun thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  2. This can cause a buyer to make a purchase on site B, and the order is replicated on site A.

    Going further, you can make sites b, c, d, replicate your order in a common site, for example, site A

    Thank you

  3. It is nice to know how a Woocommerce site A receives an order from another site B via the REST API but how the site B calls the REST API? Could you elaborate more?

    1. Hi Kelvin! Site B sends info to Site A via wp_remote_post() https://developer.wordpress.org/reference/functions/wp_remote_post/ – that’s all you need to know. Inside that wp_remote_post() we then specify the API keys, so that it can only “enter that door”. Does that help?

  4. This is great! And so simple! I guess this could be used to send data to any REST API service, not just to Woocommerce. It would be interesting to see code which reads from a REST API service.

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

Your email address will not be published.