Blog • Orders

Create a formatted order CSV

Last updated: 05 Jul 2019

This is a walkthrough how you could generate a list of your orders that include cart items.

Prerequisites

  • Moltin account with existing orders

Summary of steps required

  • Fetch your existing orders.
  • Create an array including all orders with cart items.
  • Add the array in a CSV file.

1. Get your access token

You will need to get a client_credentials access token to follow along making the API requests outlined below.

const MoltinGateway = require('@moltin/sdk').gateway

const Moltin = MoltinGateway({
	client_id: 'X',
	client_secret: 'X'
})

2. Fetch all of your order

You can use filter to only grab the orders you want. Example of that would be to filter based on time. In the example we will be grabbing them all.

const MoltinGateway = require('@moltin/sdk').gateway
//This global orders array will be used through out the example code snippets.
let orders;

const Moltin = MoltinGateway({
  client_id: 'X',
  client_secret: 'X'
})

Moltin.Orders.All().then(orders => {
  this.orders = orders
})

3. Create an array of orders and cart items

Using the above API response you will want to loop through the data and create an array that has all if the data you may need.

exports.itemsLookup(orders.data[0], orders.included.items).then((order) => {
    console.log(order.relationships.items);
});

The itemLookup function is taking an order which includes an array of items. The lookup loops through the orders and assigns the items to the id.

exports.itemsLookup = function(order, items) {
    return new Promise(function(resolve, reject) {
      
      var itemsProcessed = 0;
      let itemsArray = [];
      //for each of the orders related items
      order.relationships.items.data.forEach(function(item) {
        // ID for the orders item 
        let id = item.id;
        // simple counter
        itemsProcessed++;
        // look up each item
        items.forEach(function(item) {
          if(item.id === id) {
            console.log('matching order item object found');
            itemsArray.push(item);
            console.log(itemsArray);
          }
        });

        // if there are not order items left to process
        if(itemsProcessed === order.relationships.items.data.length) {
          console.log('finished processing items');
          order.relationships.items = itemsArray;
          resolve(order);
        };
      })
  })
};

4. Add the array to CSV

All you need to do now is to add the array to a CSV file.

Moltin.Orders
.With('items')
.All()
.then((orders) => {
	exports.formatOrders(orders, orders.included.items)
    .then((formattedOrders) => {
      console.log(formattedOrders);
    })

})
.catch((e) => {
	console.log(e);
})

To see the full code, visit GitHub.

Further reading