| 1 |
(function ($) { |
| 2 |
"use strict"; |
| 3 |
jQuery(function ($) { |
| 4 |
|
| 5 |
let totalOrders = 0; |
| 6 |
let batchSize = 0; |
| 7 |
let offset = 0; |
| 8 |
let recoverFrom = ''; |
| 9 |
let recoverTo = ''; |
| 10 |
let waitTime = 10; |
| 11 |
let processedCount = 0; |
| 12 |
|
| 13 |
|
| 14 |
$('.route-datepicker').datepicker({ |
| 15 |
dateFormat : 'yy-mm-dd' |
| 16 |
}); |
| 17 |
|
| 18 |
$('.woocommerce-save-button').html('Sync Orders'); |
| 19 |
|
| 20 |
let spinner = new jQuerySpinner({ |
| 21 |
parentId: 'wpwrap' |
| 22 |
}); |
| 23 |
|
| 24 |
// Function to process a batch of orders |
| 25 |
function processBatch() { |
| 26 |
$.ajax({ |
| 27 |
url: routeapp_ajax.ajaxurl, |
| 28 |
type: 'POST', |
| 29 |
data: { |
| 30 |
action: 'routeapp_process_orders_batch', |
| 31 |
routeapp_order_recover_from: recoverFrom, |
| 32 |
routeapp_order_recover_to: recoverTo, |
| 33 |
batchSize: batchSize, |
| 34 |
offset: offset, |
| 35 |
nonce: routeapp_ajax.nonce |
| 36 |
}, |
| 37 |
success: function (response) { |
| 38 |
if (response.success) { |
| 39 |
processedCount += response.data.processed; |
| 40 |
offset += batchSize; |
| 41 |
$('#message').html('<p> Orders processed: ' + totalOrders + '/' + processedCount + '</p>'); |
| 42 |
|
| 43 |
if (processedCount < totalOrders) { |
| 44 |
setTimeout(processBatch, waitTime * 1000); // Delay before the next batch |
| 45 |
} else { |
| 46 |
$('#message').html('<p> All orders have been processed.' + totalOrders + '/' + processedCount + '</p>'); |
| 47 |
spinner.hide(); |
| 48 |
} |
| 49 |
} else { |
| 50 |
$('#message').html('<p> ' + response.data.message + '</p>'); |
| 51 |
spinner.hide(); |
| 52 |
} |
| 53 |
}, |
| 54 |
error: function (error) { |
| 55 |
$('#message').html('<p> An error occurred while processing the orders. </p>'); |
| 56 |
spinner.hide(); |
| 57 |
} |
| 58 |
}); |
| 59 |
} |
| 60 |
|
| 61 |
$('.woocommerce-save-button').click(function (e) { |
| 62 |
e.preventDefault(); |
| 63 |
spinner.show(); |
| 64 |
|
| 65 |
$.ajax({ |
| 66 |
url: routeapp_ajax.ajaxurl, |
| 67 |
type: 'POST', |
| 68 |
data: { |
| 69 |
action: 'routeapp_save_orders', |
| 70 |
routeapp_order_recover_from: $('#routeapp_order_recover_from').val(), |
| 71 |
routeapp_order_recover_to: $('#routeapp_order_recover_to').val(), |
| 72 |
routeapp_order_recover_reconcile_backend: $('#routeapp_order_recover_reconcile_backend').is(':checked') ? 1 : 0, |
| 73 |
nonce: routeapp_ajax.nonce |
| 74 |
}, |
| 75 |
success: function (response) { |
| 76 |
if (response.success) { |
| 77 |
totalOrders = response.data.orderCount; |
| 78 |
batchSize = response.data.batchSize; |
| 79 |
recoverFrom = response.data.recoverFrom; |
| 80 |
recoverTo = response.data.recoverTo; |
| 81 |
waitTime = response.data.waitTime; |
| 82 |
processedCount = 0; |
| 83 |
offset = 0; |
| 84 |
|
| 85 |
$('#message').html('<p> Orders processed: ' + totalOrders + '/' + processedCount + '</p>'); |
| 86 |
|
| 87 |
processBatch(); |
| 88 |
} else { |
| 89 |
$('#message').html('<p> Failed to initialize order processing.</p>'); |
| 90 |
spinner.hide(); |
| 91 |
} |
| 92 |
}, |
| 93 |
error: function (error) { |
| 94 |
$('#message').html('<p> An error occurred while initiating order processing.</p>'); |
| 95 |
spinner.hide(); |
| 96 |
} |
| 97 |
}); |
| 98 |
}); |
| 99 |
|
| 100 |
|
| 101 |
}); |
| 102 |
})(jQuery); |
| 103 |
|