E-Commerce WordPress Tips & Tricks

How to Integrate WooCommerce & an External Order Management System

As orders grow, managing them inside WooCommerce alone gets harder. Here is how to integrate an external order management system, so fulfillment, inventory, and orders stay in sync as you scale.

Praveen Chauhan Praveen Chauhan 6 min read

woocommerce-order-management-integrationWhat constitutes an e-Commerce store? You have products (of course), which are the core of your store. You have associated prices, taxes, payment gateways, sales, coupons and so on. For every purchase made, you have invoices and orders. In an e-Commerce store based on WordPress, your e-Commerce plugin would manage all of the said functionality. So, WooCommerce would manage all of this.

When you think about it, managing product information, such as description, pricing, sales, are the primary responsibilities of an e-Commerce plugin like WooCommerce. Whereas, invoices, order management, shipment tracking, become part of peripheral responsibilities. WooCommerce handles products well. You can set up various types of products (some using extensions), add sale banners, link related products, categorize and tag products, and manage purchases using various payment methods. It makes the task of a buyer real simple.

When it comes to admin activities, the task is a bit tricky, because your options are limited. Several WooCommerce store owners prefer integrating external services. For example, a client needed e-conomic, an external accounting system, to be integrated with WooCommerce. This was achievable, because e-conomic provided the needed API.

Similarly, if a system has a ready API, you could integrate it with the WooCommerce plugin. In this article, we’ll explore how to integrate an external order management system with WooCommerce.

[space]

Integrating WooCommerce with an Order Management System

When it comes to order management, the data which is part of a WooCommerce order, should be sent across to the external system. Let’s assume, that your Order Management system offers an API, which accepts order details (as listed below) and processes it.

Details which can be sent to the external system:

  • Customer e-mail (customer_email)
  • Customer Phone (customer_phone)
  • 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)

[space]

Send Order Details to an External System

To send details from WooCommerce to this external order management system, you have to use cURL. 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', 'wdm_send_order_to_ext'); 
function wdm_send_order_to_ext( $order_id ){
    // get order object and order details
    $order = new WC_Order( $order_id ); 
    $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();
    
    $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;   
    
    // set the username and password
    $api_username = 'testuser';
    $api_password = 'testpass';

    // to test out the API, set $api_mode as ‘sandbox’
    $api_mode = 'sandbox';
    if($api_mode == 'sandbox'){
        // sandbox URL example
        $endpoint = "http://sandbox.example.com/"; 
    }
    else{
        // production URL example
        $endpoint = "http://example.com/"; 
    }

        // setup the data which has to be sent
    $data = array(
            'apiuser' => $api_username,
            'apipass' => $api_password,
            '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,
            'coupon_code' => implode( ",", $coupon )
        );

            // send API request via cURL
        $ch = curl_init();

        /* set the complete URL, to process the order on the external system. Let’s consider http://example.com/buyitem.php is the URL, which invokes the API */
        curl_setopt($ch, CURLOPT_URL, $endpoint."buyitem.php");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
        $response = curl_exec ($ch);
    
        curl_close ($ch);
        
        // the handle response    
        if (strpos($response,'ERROR') !== false) {
                print_r($response);
        } else {
                // success
        }
 }

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 system (and not vice versa). To send data back to WooCommerce, you will need to make use of additional API, the system provides.

[space]

Do you intend to use an external Order Management system 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.

[freepik]

Get a FREE Consultation

Let's build something that lasts.

Share what's on your mind — a clear brief, a half-formed idea, or just a sense that something needs to change. We'll listen first, ask the right questions, and point you toward what's actually worth building.

We take on a handful of projects each quarter,ones where we can truly make a difference.

  • Receive a human response within 24 hours
  • Get a detailed scope and quote upfront
  • We're happy to sign an NDA upon request

    Free 30-Min Strategy Call

    Your Name *

    Your Phone No *

    Work Email *

    Your Budget*

    Project Details *