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