WooCommerce: Create An Order From Another Website!

Fundamentally I was searching for a way of making a request on  WooCommerce site when a client enlisted from the My Account page on the WooWeekly WooCommerce site. The justification for doing that will be that I’m utilizing email advertising on site, and the best way to add an email contact from one more site was by utilizing the “REST API” that WooCommerce gives.

Presently, I took in this today, so you can figure out how to accomplish complex stuff as well. I’ll simply save you two or three hours of migraines attempting to sort out how the framework functions – that is the reason you’re here!

Things being what they are, how would you make a WooCommerce request on one site when an occasion happens on another site?

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 “client key” and a “client secret”:

Presently, your site where you wish to make requests (or anything for example WooCommerce items, clients and so forth) is “prepared” as we just made a way for one more site to speak with it, peruse and compose.

2. Add code to the “other” website

When something occurs on the other site for example a structure is submitted, you can utilize some code to “send the information” to the site where you need to make the request automatically.

For my situation I’ve chosen the “woocommerce_created_customer” snare: when a WooCommerce client registers on https://wcwkly.com (see the enrollment structure on top of the landing page?) I need a request made on site I have made API keys on site, so I can interface the two and let them do something amazing.

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

 

add_action( 'woocommerce_created_customer', 'create_order_wcwkly', 9999, 3 ); 
    
function create_order_wcwkly( $customer_id, $new_customer_data, $password_generated ) {
   $live_ck = 'ck_blablabla';
   $live_cs = 'cs_blablabla';
   $live_url = 'https://www.demo.com/wp-json/wc/v3/orders?client_key=' . $live_ck . '&client_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 ),
      )
   );
}