Search code examples
node.jsshopifynode-fetch

Shopify REST Admin API - fetch orders by line_items[0].title = something


For context, I am requesting via node-fetch the Shopify Admin REST API orders.json with the queries shown in my code. For some reason, this only returns the latest order regardless of if I add status=any or limit=250 (which is the standard limit for orders). Before I go too much into detail let me show my code so you can understand the query.

const User = require('../models/User.model');
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));

const getReportsPartner = async (req, res, next) => {
    const user = await User.findById(req.user.id)
    const url = 'https://astore.com/admin/orders.json?fields=total_price_usd,line_items';
    const headers = {
        'X-Shopify-Access-Token': 'anaccesstoken',
        'Content-type': 'application/json',
        'Authorization': 'anauthkey'
    }

    fetch(url, { method: 'GET', headers: headers })
        .then(res => res.json())
        .then(json => {
            const orders = json.orders[0]
            console.log(orders)
            const partner = orders.line_items[0].title
            const commission = (orders.total_price_usd * .125).toFixed(2)
            res.render('reports/partnerReport', { partner: partner, commission: commission })
        })
}

module.exports = { getReportsPartner }

I have attempted modifying the URL in a few ways other than status and limit. I will list some of them below: https://astore.com/admin/orders.json?fields=total_price_usd,line_items.title=something https://astore.com/admin/orders.json?fields=total_price_usd,line_items&line_items.title=something

I have also tried line_items[0].title=something in the url as well, however this seems to invalidate the entire request. I appreciate any help you can give. If you need more in terms of context let me know. Ultimately I plan to get recent orders by title and graph the orders and commissions in a report format. For future orders I will be using a webhook, so at the moment I am only worried about past.


Solution

  • As HymnZzy mention in the comments, it is not possible to query for only line_items title. What I have decided to do is make requests for the last 250 orders or from a certain date range. Here is an example url with query.

    `https://${yourshop}.com/admin/api/2022-01/orders.json?limit=250&status=any&financial_status=paid&fields=line_items,total_price_usd`
    

    Then I loop through the results and do something with each order with a certain title.

    for (let i=0; i<json.orders.length; i++) {
                  try {
                  let title = json.orders[i].line_items[0].title
                  let price = json.orders[i].total_price_usd
                  /* Do Something with title and price here */
                } catch (err) {
                  console.log(err)
                }
              } 
    

    My only issue is the speed of this method, however it seems Shopify has limited us to something like this if we would like to find orders by line_items title via the REST Admin API.