Order API - Retrieve orders INCLUDING the one stated in since_id

Solved

Order API - Retrieve orders INCLUDING the one stated in since_id

DRight
Shopify Partner
19 1 0

I'm working on an extension that exports orders into a CSV file.

Currently, I'm having an issue with how 'since_id' is working because I need to be able to retrieve X orders since the very first one (#1001) but that's not possible since there's no order #1000 thus I can only get all the orders after #1001, but in that case #1001 will not be included in the CSV file.

Any suggestions?

Accepted Solution (1)

Jayvin
Shopify Partner
284 42 89

This is an accepted solution.

Hi,

Hmm, a simple solution for your workflow can be something like this:

  1. Get the first single order using with the 'since_id', like this '/orders/${since_id}.json'
  2. Get all the orders using '/orders.json?since_id=${since_id}'
  3. Merge the single order with all the orders, prepend it to the second orders array
  4. Generate your CSV file

 

banned

View solution in original post

Replies 3 (3)

Jayvin
Shopify Partner
284 42 89

This is an accepted solution.

Hi,

Hmm, a simple solution for your workflow can be something like this:

  1. Get the first single order using with the 'since_id', like this '/orders/${since_id}.json'
  2. Get all the orders using '/orders.json?since_id=${since_id}'
  3. Merge the single order with all the orders, prepend it to the second orders array
  4. Generate your CSV file

 

banned
DRight
Shopify Partner
19 1 0

Thanks, I was trying to take a similar approach to what you've suggested without luck before.

Was trying to make it work this way again but still, it only exports 1002-1005 even though I concat the arrays and my final array has order #1001 added to the end.

Here's some code:

 

  setTimeout(function() {
    var url = formatUrl(orderId, page, limit);

    $.get(url).then(function(response) {
      var orders = response.orders;

      if (orders.length) {
        allOrders = allOrders.concat(orders);

        if (including_first === 1) {
          var url_first = formatUrlForFirstOrder(orderId);

          $.get(url_first).then(function (response) {
            var first_order = response.order;
            allOrders = allOrders.concat(first_order);

            console.log(JSON.stringify(allOrders)); // console.log the final array to see if it was successfully combined
          });
        }

        if (
            allOrders.length <= limit
        ) {
          exportCSV(allOrders, filters);
        }
    });
  }, 100);
}

 

 

Any ideas?

DRight
Shopify Partner
19 1 0

Edit: It took a few seconds to build that new array, and it didn't work because I was sending the old array before the new one was formed.

It works now after adding setTimeout.

Thanks.