Magento 2, Exporting Orders Through REST

We have a an Inventory and Order Management system that is tailored towards our business. It was written several years ago and has been evolving ever since. Magento has good order management built in, but we need more. Shipping, customer tracking, order sourcing and other key features don’t exist for us inside Magento. Exporting orders is a key function of the framework we rely on daily.

Where do I start?

If you haven’t already created an API integration inside Magento 2, please see my post “Magento 2 REST API: Introduction” to get that going.

Now that you have that setup, let’s break this down a little bit.

1. Authenticate through the REST API (setup HTTP headers).
2. Send over your JSON request.
3. Parse your JSON response.
4. Save your orders to your database.

The Code Behind

I like breaking up all of my crons into either classes or at least functions. The first function you need is a way to authenticate. When you created your integration you received an access token. You’ll want to plug that into the headers.

Here’s a PHP function that will send the request and authorization.

public function getOrders( $status )
{
    $ch = curl_init( "http://www.yourwebsite.com/index.php/rest/V1/orders?searchCriteria[filterGroups][0][filters][0][field]=status&searchCriteria[filterGroups][0][filters][0][value]=" . $status );

    $curlOptions = array(
        CURLOPT_CUSTOMREQUEST  => "GET",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER     => array(
            "Content-type: application/json",
            "Authorization: bearer access-token-from-magento"
        )
    );

    curl_setopt_array( $ch, $curlOptions );

    $response = curl_exec( $ch );

    return $response;
}

Let’s break this down a bit. The first thing you’re going to do is initialize curl, giving it the URL for the REST API specified in the Magento 2 REST API docs. After that, you need some options for curl. For grabbing orders, make sure you’re doing a GET request. Finally, you execute your curl request and grab the response.

The $status is something to take note of. That is the status of orders that you want to grab. Let’s take a look at how to use this.

$newOrdersJson = getOrders( "processing" );

$newOrders = json_decode( $newOrdersJson, true );

print_r( $newOrders );

Line 1 has the argument for the function set to “processing”. This is the state of the orders after someone successfully purchases a product with their credit card. PO Orders and Check/Money orders will be “Pending” until you invoice them. Make sure you type this in all lowercase. It is in the format that is in the database, not what is visible on the order.

From here I simply converted the JSON to an associative array, and then did a print_r() to look at it.

This is where you’ll want to take a careful look through the array. You can pull up the order in Magento’s back-end to make sure you’re grabbing what you want. All the information about the order is in there.

Challenges

The only challenge I found were sorting out the purpose of the row_id and the increment_id.

increment_id = The order number and the number that the customer sees.
row_id = The row’s id column.

You’ll want to store both in your OMS. One for referencing customer orders, and one for updating the state of the order from your OMS. We update shipping information and set Processing to Complete when things are shipped from our warehouse. This request will want the row_id.

Summary

Exporting orders is a fairly simple process with Magento 2’s REST API. You get a json response that you can easily convert to an object or array. From there, the magic for your OMS or ERP comes with your imagination. Study the response and test thoroughly. After that, set your cron to run every few minutes and you’ll be integrated in no time.

-Dan

My Tools –

Lenovo Y700

HP Pavilion 23xw 23-in IPS LED Backlit Monitor

Logitech MK270 Wireless Keyboard/Mouse

Written by Dan
Welcome to my blog! Here you'll find my collection of bible study lessons, book reviews, and other posts I feel inspired to write. I am a Christian currently serving at First Baptist Church of Northville in Northville, MI.