POST is used to send data to the server for the server to act upon in some way. For example, a Woocommerce order. When you order products on any online store after that the browser takes the data and sends a POST request to the server with the order that you have placed. From there the server will process the order request.
In our case will send this data to any other server or third-party API by using the most popular method (wp_remote_post) provided by WordPress.
wp_remote_post( string $url, array $args = array() ): array|WP_Error
Performs an HTTP request using the POST method and returns its response.
Integrating WooCommerce with an Order Management API
When it comes to ordering management API, the data which is part of a WooCommerce order should be sent across to the external system. Let’s assume, that you’re using a third-party system for managing orders and they offer an API, which accepts order details (as listed below) and processes them.
Details that can be sent to the external system:
- Customer e-mail (customer_email)
- Customer Phone (customer_phone)
- Product Details
- Product Variations Details
- Billing Details
- First Name (bill_firstname)
- Last Name (bill_surname)
- Address Line 1 (bill_address1)
- Address Line 2 (bill_address2)
- City (bill_city)
- State (bill_state)
- ZIP Code (bill_zip)
- Shipping Details
- First Name (ship_firstname)
- Last Name (ship_surname)
- Address Line 1 (ship_address1)
- Address Line 2 (ship_address2)
- City (ship_city)
- State (ship_state)
- ZIP Code (ship_zip)
- Shipping Method (shipping_type)
- Shipping Cost (shipping_cost)
- Item SKU (item_sku)
- Item Price (item_price)
- Quantity (quantity)
- Transaction ID/Key (transaction_key)
- Coupon Code (coupon_code)
By using this method we can send our woo-commerce orders to the external API.
To send details from WooCommerce to this external order management API, you have to use the wp_remote_post() method. For this, use the below code, by adding it to functions.php of your theme or a custom plugin (recommended).
/* after an order has been processed, we will use the 'woocommerce_thankyou' hook, to add our function, to send the data */
add_action('woocommerce_thankyou', 'qlc_send_order_to_ext');
function qlc_send_order_to_ext( $order_id ){
// get order object and order details
$order = new WC_Order( $order_id );
$order_id = $order->id;
$customer_id = $order->customer_id;
$payment_method = $order->payment_method;
$order_total_amount = $order->total;
$customer_note = $order->customer_note;
$email = $order->billing_email;
$phone = $order->billing_phone;
$shipping_type = $order->get_shipping_method();
$shipping_cost = $order->get_total_shipping();
// set the address fields
$user_id = $order->user_id;
$address_fields = array(
'country',
'title',
'first_name',
'last_name',
'company',
'address_1',
'address_2',
'address_3',
'address_4',
'city',
'state',
'postcode');
$address = array();
if(is_array($address_fields)){
foreach($address_fields as $field){
$address['billing_'.$field] = get_user_meta( $user_id, 'billing_'.$field, true );
$address['shipping_'.$field] = get_user_meta( $user_id, 'shipping_'.$field, true );
}
}
// get coupon information (if applicable)
$cps = array();
$cps = $order->get_items( 'coupon' );
$coupon = array();
foreach($cps as $cp){
// get coupon titles (and additional details if accepted by the API)
$coupon[] = $cp['name'];
}
// get product details
$items = $order->get_items();
$product_options = array();
$items_array = array();
// get product Variations details
foreach ( $items as $item_id => $item_values ) {
$option_data = $item_values->get_meta_data();
foreach ( $option_data as $options_key => $options_values ) {
$option_sep_values = $options_values->get_data();
$option_id = $option_sep_values['id'];
$option_key = $option_sep_values['key'];
$option_value = $option_sep_values['value'];
$product_options[] = array(
'name' => $option_id,
'key' => $option_key,
'rate' => $option_value,
'quantity' => 'NULL',
'subtotal' => $option_value,
'discountPercentage' => 0,
'discountAmount' => 0,
'taxRate' => 0,
'taxAmount' => 0,
'serviceChargeExempt' => 0,
'serviceChargePercentage' => 0,
'serviceChargeAmount' => 0,
'taxIncluded' => 1,
'netAmount' => $option_value
);
}
// OR the Product id from the item data
$item_data = $item_values->get_data();
$product_name = $item_data['name'];
$product_id = $item_data['product_id'];
$product_subtotal = $item_data['subtotal'];
$product_quantity = $item_data['quantity'];
$product_total = $item_data['total'];
$product_taxes_total = $item_data['total_tax'];
$product_taxes_subtotal = $item_data['subtotal_tax'];
$items_array[] = array(
'product_name' => $product_name,
'rate' => $product_total,
'quantity' => $product_quantity,
'subtotal' => $product_subtotal,
'netAmount' => $product_subtotal,
'options' => $product_options // Product variation options array
);
$product_options = array();
}
$item_name = array();
$item_qty = array();
$item_price = array();
$item_sku = array();
foreach( $items as $key => $item){
$item_name[] = $item['name'];
$item_qty[] = $item['qty'];
$item_price[] = $item['line_total'];
$item_id = $item['product_id'];
$product = new WC_Product($item_id);
$item_sku[] = $product->get_sku();
}
/* for online payments, send across the transaction ID/key. If the payment is handled offline, you could send across the order key instead */
$transaction_key = get_post_meta( $order_id, '_transaction_id', true );
$transaction_key = empty($transaction_key) ? $_GET['key'] : $transaction_key;
// to test out the API, set $api_mode as ‘sandbox’
$api_mode = 'sandbox';
if($api_mode == 'sandbox'){
// sandbox URL example
$endpoint = "https://api.example.com/sandbox";
}
else{
// production URL example
$endpoint = "https://api.example.com/production";
}
// set the username and password
$api_username = 'testuser';
$api_password = 'testpass'
// setup the data which has to be sent
$data = array(
'apiuser' => $api_username,
'apipass' => $api_password,
'id' => $order_id,
'items' => $items_array, // Items array define above
'payment' => array(
'type' => $payment_method,
'amount' => $order_total_amount
),
'customer_email' => $email,
'customer_phone' => $phone,
'bill_firstname' => $address['billing_first_name'],
'bill_surname' => $address['billing_last_name'],
'bill_address1' => $address['billing_address_1'],
'bill_address2' => $address['billing_address_2'],
'bill_city' => $address['billing_city'],
'bill_state' => $address['billing_state'],
'bill_zip' => $address['billing_postcode'],
'ship_firstname' => $address['shipping_first_name'],
'ship_surname' => $address['shipping_last_name'],
'ship_address1' => $address['shipping_address_1'],
'ship_address2' => $address['shipping_address_2'],
'ship_city' => $address['shipping_city'],
'ship_state' => $address['shipping_state'],
'ship_zip' => $address['shipping_postcode'],
'shipping_type' => $shipping_type,
'shipping_cost' => $shipping_cost,
'item_sku' => implode(',', $item_sku),
'item_price' => implode(',', $item_price),
'quantity' => implode(',', $item_qty),
'transaction_key' => $transaction_key,
'customerNotes' => $customer_note,
'coupon_code' => implode( ",", $coupon )
);
//echo json_encode($data);
$response = wp_remote_post( $endpoint, array(
'method' => 'POST',
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'timeout' => 60,
'sslverify' => false,
'data_format' => 'body',
'body' => $data,
'cookies' => array()
)
);
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
echo "Something went wrong: $error_message";
} else {
echo 'Response:<pre>';
print_r( $response );
echo '</pre>';
}
The above code is a very basic example. You can enhance it, based on the APIs available, or as per your requirements. Also, this can be used only to send order details from WooCommerce to an external API (and not vice versa). To send data back to WooCommerce, you will need to make use of additional API, the system provides.
Do you intend to use an external Order Management API with WooCommerce? Hope this article can get you started. If you have any doubts, you can write them to me, via the comment section below.