WooCommerce Rest API get all orders only return 10 results in React Native

Viewed 869

I'm doing a mobile application to React Native to read the orders in Wordpress/WooCommerce. I'm using a wrapper called react-native-woocommerce-api.

I'm trying to get all the orders but, the call only returns 10 results. According to what I read, this is by default. I saw solutions using PHP and Python but, I don't know how to replicate this in JavaScript and with the wrapper that I'm using.

Function to get all the orders:

getAllOrders = () => {
    OrderAction.getAll().then(async function (response) {
      console.log(response);

      let orders = null;

      if (response === false) {
        self.setState({error: 'Error'});
      } else if (response.code) {
        self.setState({error: response.message});
      } else {
        orders = response;
      }

    });
  };

The call using the wrapper:

export async function getAll() {
  try {
    let response = await WooCommerce.get(Constants.STORE.orders, {});
    return response;
  } catch (error) {
    console.log(error);
  }
}

Reading the WooCommerce documentation and the solutions in other languages that I have found, I need to include in the call parameters, like per_page or page but, when I try to do it, I receive an error by the IDE.

Example:

export async function getAll() {
  try {
    let response = await WooCommerce.get(Constants.STORE.orders, {
    {
      'per_page', 5000
    }
    });
    return response;
  } catch (error) {
    console.log(error);
  }
}

statement expected

Trying to do it adding per_page and page at the end of the url:

export async function getAll() {

  let allOrders = [], page = 1;

  while (page !== false) {

    try {

      const orders = await WooCommerce.get(Constants.STORE.orders + '?per_page=100&page=' + page, {});

      if (orders.length) {
        allOrders = allOrders.concat(orders);
        page++;
      } else {
        page = false; // last page
      }

    } catch (error) {
      console.log(error);
    }

  }

  return allOrders;
}

rest_invalid_param

Maybe the wrapper doesn't support parameters? I can't find an example in the wrapper documentation either.

My goal is to get all the orders where the status is 'completed' to change them to 'processing', but, I need to be able to get all the orders first.

0 Answers
Related