Change Woocommerce checkout to process order on an separate WordPress site?
Try this way
Add this code in site A.
function action_woocommerce_new_order( $order_id ) {
//get all order details by $order_id
//post your data to site B
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://yoursiteB.com/wp-json/api/v2/create_wc_order");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"postvar1=value1&postvar2=value2");
// In real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS,
// http_build_query(array('postvar1' => 'value1')));
// Receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);
};
add_action( 'woocommerce_payment_complete', 'action_woocommerce_new_order', 10, 1 );
Add this code in site B.
Class WC_Rest_API {
public function __construct() {
add_action( 'rest_api_init', array( $this, 'init_rest_api') );
}
public function init_rest_api() {
register_rest_route('api/v2', '/create_wc_order', array(
'methods' => 'POST',
'callback' => array($this,'create_wc_order'),
));
}
public function create_wc_order( $data ) {
//$data - all the details needs to be send from site A
global $woocommerce;
$user_id = $data['user_id'];
$user = get_user_by( 'ID', $user_id);
$phonenumer = get_user_meta( $user_id,'user_phoneno', true);
if($user) {
$address = array(
'first_name' => $user->display_name,
'last_name' => $user->display_name,
'email' => $user->user_email,
'phone' => $phonenumer,
);
$order = wc_create_order();
//get product details from rest api site A
$order->add_product();
$order->set_address( $address, 'billing' );
$order->calculate_totals();
$order = wc_get_order( $order->id );
$response['status'] = 'success';
$response['message'] = 'Order created on wordpress site B.';
return new WP_REST_Response($response, 200);
}
}
}
$rest_api = new WC_Rest_API();
Send all the data from site A to site B via below the URL.
http://yoursiteB.com/wp-json/api/v2/create_wc_order
Method should be 'POST'
Data should be contain all the details od order from site A.
Let me know if you have any question.