woocommerce-orders-google-sheets-integration icon indicating copy to clipboard operation
woocommerce-orders-google-sheets-integration copied to clipboard

Sync 100 orders only

Open Sushant3004 opened this issue 4 years ago • 5 comments

Hello mithunmanohar, Code is excellent but i am getting only 100 orders in sheets. I want orders from entered date to current date. how i get please help me.

Sushant3004 avatar Sep 18 '19 06:09 Sushant3004

This is a known issue. Currently the script can only pull maximum of 100 orders.

mithunmanohar avatar Sep 20 '19 06:09 mithunmanohar

the odd part is that it appears to actually pull more than 100 orders... BUT, the sheet itself appears to be limited to 100 order rows.

splaquet avatar Dec 24 '19 16:12 splaquet

I managed to fix this by doing a loop for all the pages. Before Logger.log(result.getResponseCode()) I added this:

    var headers = result.getAllHeaders();
  var total_pages = headers['x-wp-totalpages'];
    var pages_count = 0;
    Logger.log("TOTALPAGES: "+headers['x-wp-totalpages']);

  while (pages_count < total_pages) {

and after

        Logger.log(params[i]);

        removeDuplicates(sheet_name);
    }

I added this:

pages_count++; 
    if (pages_count < total_pages)
    {
    url = website + "/wp-json/wc/v2/orders?consumer_key=" + ck + "&consumer_secret=" + cs + "&after=" + m + "&per_page=100&page="+(pages_count+1); 
    Logger.log(url)

    var options =

        {
            "method": "GET",
            "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
            "muteHttpExceptions": true,

        };

    var result = UrlFetchApp.fetch(url, options);      
      
    }

It's probably not the best solution, but it worked for me.

iwasthesword avatar May 28 '20 20:05 iwasthesword

I managed to fix this by doing a loop for all the pages. Before Logger.log(result.getResponseCode()) I added this:

    var headers = result.getAllHeaders();
  var total_pages = headers['x-wp-totalpages'];
    var pages_count = 0;
    Logger.log("TOTALPAGES: "+headers['x-wp-totalpages']);

  while (pages_count < total_pages) {

and after

        Logger.log(params[i]);

        removeDuplicates(sheet_name);
    }

I added this:

pages_count++; 
    if (pages_count < total_pages)
    {
    url = website + "/wp-json/wc/v2/orders?consumer_key=" + ck + "&consumer_secret=" + cs + "&after=" + m + "&per_page=100&page="+(pages_count+1); 
    Logger.log(url)

    var options =

        {
            "method": "GET",
            "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
            "muteHttpExceptions": true,

        };

    var result = UrlFetchApp.fetch(url, options);      
      
    }

It's probably not the best solution, but it worked for me.

It looks good but I can't seem to get this to work, would you be able to show the full script?

michaelkotlyar avatar Oct 22 '20 22:10 michaelkotlyar

It looks good but I can't seem to get this to work, would you be able to show the full script?

I changed a bit the columns order/logic to fit my needs, but this a working copy with multiple page support plus I added the "removeDuplicates" to the end of the process so it wouldn't timeout, it will auto sort column 13 (just comment out line 227) and the manual date is replaced by automatic dates (lines 29 and 33, between five days before and after =TODAY).

https://gist.github.com/iwasthesword/7679a3ac437df36fc8a492631e8f3efa

iwasthesword avatar Nov 17 '20 20:11 iwasthesword