WooCommerce: Create An Order From Another Website!
VIA BUSINESS BLOOMERS

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”:

"use strict"; var adace_load_61469f03ae095 = function(){ var viewport = $(window).width(); var tabletStart = 601; var landscapeStart = 801; var tabletEnd = 961; var content = '%3Cdiv%20class%3D%22adace_adsense_61469f03adeed%22%3E%3Cscript%20async%20src%3D%22%2F%2Fpagead2.googlesyndication.com%2Fpagead%2Fjs%2Fadsbygoogle.js%22%3E%3C%2Fscript%3E%0A%09%09%3Cins%20class%3D%22adsbygoogle%22%0A%09%09style%3D%22display%3Ablock%3B%22%0A%09%09data-ad-client%3D%22ca-pub-9250614219462833%22%0A%09%09data-ad-slot%3D%224692840435%22%0A%09%09data-ad-format%3D%22horizontal%22%0A%09%09%3E%3C%2Fins%3E%0A%09%09%3Cscript%3E%28adsbygoogle%20%3D%20window.adsbygoogle%20%7C%7C%20%5B%5D%29.push%28%7B%7D%29%3B%3C%2Fscript%3E%3C%2Fdiv%3E'; var unpack = true; if(viewport=tabletStart && viewport=landscapeStart && viewport=tabletStart && viewport=tabletEnd){ if ($wrapper.hasClass('.adace-hide-on-desktop')){ $wrapper.remove(); } } if(unpack) { $self.replaceWith(decodeURIComponent(content)); } } if($wrapper.css('visibility') === 'visible' ) { adace_load_61469f03ae095(); } else { //fire when visible. var refreshIntervalId = setInterval(function(){ if($wrapper.css('visibility') === 'visible' ) { adace_load_61469f03ae095(); clearInterval(refreshIntervalId); } }, 999); }

})(jQuery);

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 . 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.

WooCommerce: Create An Order From Another Website! .