I need to create how Woocommerce handles orders so I can create an order on Site A
and send that order to Site B
.
So when customer adds items to their cart and click checkout, it creates the order on both Site A
and Site B
, however, it also redirects the user to Site B
to process the payment.
So far I can only change the checkout button:
add_filter( 'woocommerce_get_checkout_url', 'my_change_checkout_url', 30 );
function my_change_checkout_url( $url ) {
$url = "your checkout url ";
return $url;
}
and creating the order.
if (isset($_POST['isOrder']) && $_POST['isOrder'] == 1) {
$address = array(
'first_name' => $_POST['notes']['domain'],
'last_name' => '',
'company' => $_POST['customer']['company'],
'email' => $_POST['customer']['email'],
'phone' => $_POST['customer']['phone'],
'address_1' => $_POST['customer']['address'],
'address_2' => '',
'city' => $_POST['customer']['city'],
'state' => '',
'postcode' => $_POST['customer']['postalcode'],
'country' => 'NL'
);
$order = wc_create_order();
foreach ($_POST['product_order'] as $productId => $productOrdered) :
$order->add_product( get_product( $productId ), 1 );
endforeach;
$order->set_address( $address, 'billing' );
$order->set_address( $address, 'shipping' );
$order->calculate_totals();
update_post_meta( $order->id, '_payment_method', 'ideal' );
update_post_meta( $order->id, '_payment_method_title', 'iDeal' );
// Store Order ID in session so it can be re-used after payment failure
WC()->session->order_awaiting_payment = $order->id;
// Process Payment
$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
$result = $available_gateways[ 'ideal' ]->process_payment( $order->id );
// Redirect to success/confirmation/payment page
if ( $result['result'] == 'success' ) {
$result = apply_filters( 'woocommerce_payment_successful_result', $result, $order->id );
wp_redirect( $result['redirect'] );
exit;
}
}
Though I'm not sure how to put all of this together so I can work with both Site A and Site B.
How can I do this?
EDIT: To explain more what I need this to do, I need to process payments on a second website, where it also has the products.
So, Customer buys product on Site A, after clicking "Pay with Paypal" (Instant Checkout) and "proceed with payment", the customer and it's information it filled is taken to Site B, where their either able to finalize the payment, OR be directly taken to the Paypal gateway on Site B.
You can create multiple Checkout pages by adding the [woocommerce_checkout] shortcode to each page, and you should be able to use them to Check out, however by default you will only be able to set a single one under WooCommerce > Settings > Advanced.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With