MONEI / Shopify-api-node

Node Shopify connector sponsored by MONEI
https://monei.com/shopify-payment-gateway/
MIT License
946 stars 277 forks source link

Fetch all orders instead of only 250 ? #382

Closed markus-roe closed 4 years ago

markus-roe commented 4 years ago

Hi,

Is there a way of fetching all orders from the shopify store? The current limit is 250, how can I fetch all?

lpinca commented 4 years ago

See https://github.com/MONEI/Shopify-api-node#pagination

markus-roe commented 4 years ago

See https://github.com/MONEI/Shopify-api-node#pagination

This is not working for ORDERS as it seems.. :/

lpinca commented 4 years ago

It should, use a specific (recent) api version.

markus-roe commented 4 years ago

It should, use a specific (recent) api version.

It seems like nextPageParameters is not working for orders..

    (async () => {
        let params = { limit: 10 };
        let orderArray = [];

        do {
            const orders = await shopify.order.list(params);
            orderArray.push(orders);
            params = orders.nextPageParameters;
            console.log('params:', params);
        } while (params !== undefined);
        console.log(orderArray[0].length);

        res.send(orderArray);
    })().catch(console.error);

I get the output:

params: undefined
10
lpinca commented 4 years ago

It works.

const Shopify = require("shopify-api-node");

const shopify = new Shopify({
  shopName: "quuz",
  apiVersion: "2020-04",
  apiKey: "b68c3a4bf44cd240511e5f4114f0be4d",
  password: "2d170167fc62551920ca60fe40a5e548",
});

(async function () {
  const product = await shopify.product.create({
    title: "Burton Custom Freestlye 151",
    body_html: "<strong>Good snowboard!</strong>",
    vendor: "Burton",
    product_type: "Snowboard",
  });

  const data = {
    line_items: [
      {
        variant_id: product.variants[0].id,
        quantity: 1,
      },
    ],
  };

  await Promise.all([
    shopify.order.create(data),
    shopify.order.create(data),
    shopify.order.create(data),
  ]);

  const allOrders = [];
  let params = { limit: 1, status: 'any' };

  do {
    const orders = await shopify.order.list(params);
    allOrders.push(...orders);
    params = orders.nextPageParameters;

    console.log("params: ", params);
  } while (params !== undefined);

  console.log(allOrders.length);

  await Promise.all([
    shopify.product.delete(product.id),
    ...allOrders.map((order) => shopify.order.delete(order.id)),
  ]);
})().catch(console.error);
kamranblueeast commented 3 years ago

@lpinca, Whenever I passed page_info with updated_at_min, updated_at_max to fetch orders it returns as Bad Request(400), Otherwise it is working fine. Any help?

lpinca commented 3 years ago

You have to take orders.nextPageParameters as is and pass it to orders.list(). page_info is atomatically generated and included in orders.nextPageParameters.

kamranblueeast commented 3 years ago

@lpinca, thanks I will try this.:)