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
* @community https://businessbloomer.com/club/
*/
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.
Hello! I would like to know if it can be done as a client, and not as a guest. Thank you!
I believe you need to add a ‘customer_id’ line as well, and that should assign the order to an existing customer
My question is for paid orders. If the order is for items to be paid for, which site does the actual payment processing and gets the funds? Does everything happen on Website B, and the order is just added to Website A as completed to be fulfilled?
Thanks for the code/tutorial.
Hey Josh, I use this for free orders, so I didn’t really think about payments.
You can accept payments on either website, but it’s much easier to accept them on Website A where customer adds to cart and pays. Then, you programmatically create a “processing” order on Website B, and fulfill from there.
This is so good, it took hours in my case to understand that 3 lines of code* truly enough for proper, authenticated request (and the fact that i needed to whitelist my ”other” wp site’s IP on the Woo site’s host environment, to pass an additional ‘folder lock’ and to finally eliminate the 401 error back from the woo REST api 🙂 ..
My code, simplified for getting a specific product:
And as i see ‘init’ is already a safe WP hook to use these REST requests, by the way.
Maybe helps someone
Love you works, best wishes, Wiktor
Thanks!
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.
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!
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
Cool
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?
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?
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.
Thank you