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

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Get Order Info (total, items, etc) From $order Object
    As a WooCommerce development freelancer, every day I repeat many coding operations that make me waste time. One of them is: “How to get ____ if I have the $order variable/object?“. For example, “How can I get the order total“? Or “How can I get the order items“? Or maybe the order dates, customer ID, […]
  • WooCommerce: How to Add a Custom Checkout Field
    Let’s imagine you want to add a custom checkout field (and not an additional billing or shipping field) on the WooCommerce Checkout page. For example, it might be a customer licence number – this has got nothing to do with billing and nothing to do with shipping. Ideally, this custom field could show above the […]
  • WooCommerce: Allow Users to Edit Processing Orders
    How can WooCommerce customers edit an order they just placed and paid for? I swear I looked on search engine results and other places before coming to the conclusion I needed to code this myself. For example, a user might want to change the delivery date (if you provide this on the checkout page). Or […]
  • WooCommerce: Create a Custom Order Status
    All WooCommerce orders go to either “processing”, “completed”, “on-hold” and other default order statuses based on the payment method and product type. Sometimes these statuses are not enough. For example, you might need to mark certain orders in a different way for tracking, filtering, exporting purposes. Or you might want to disable default emails by […]
  • WooCommerce: Add Column to Orders Table @ WP Dashboard
    The WooCommerce Orders Table, which can be found under WP Dashboard > WooCommerce > Orders, provides us with 7 default columns: Order – Date – Status – Billing – Ship to – Total – Actions. This is used by shop managers to have an overview of all orders, before eventually clicking on a specific one. […]

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. Follow @rmelogli

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

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

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

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

    $url = 'https://somewoowebsite.com/wp-json/wc/v3/products/12345?consumer_key=ck_blablaa&consumer_secret=cs_blablaaa;
    $raw_response = wp_remote_get($url);
    $product_obj = json_decode($raw_response['body']);
    

    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

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

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

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

  6. 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? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. 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 the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *