woocommerce-orders-google-sheets-integration
woocommerce-orders-google-sheets-integration copied to clipboard
Sync 100 orders only
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.
This is a known issue. Currently the script can only pull maximum of 100 orders.
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.
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.
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?
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