Optimization of order unloading.
This commit is contained in:
parent
cd57de01e6
commit
fe64c3a3f9
2 changed files with 58 additions and 9 deletions
|
@ -602,6 +602,32 @@ if (!class_exists('WC_Retailcrm_Orders')) :
|
|||
);
|
||||
}
|
||||
|
||||
public function processOrderForUpload($orderIds)
|
||||
{
|
||||
$ordersForUpload = [];
|
||||
$errorOrders = [];
|
||||
|
||||
foreach ($orderIds as $orderId) {
|
||||
try {
|
||||
$this->order = [];
|
||||
$this->processOrder(wc_get_order($orderId));
|
||||
|
||||
if ($this->order === []) {
|
||||
throw new \RuntimeException(sprintf('Order %s is not uploaded', $orderId));
|
||||
}
|
||||
|
||||
$ordersForUpload[] = $this->order;
|
||||
} catch (Throwable $exception) {
|
||||
$errorOrders[$orderId] = sprintf(
|
||||
'Exception for Order [%s]: %s. Trace: %s',
|
||||
$orderId, $exception->getMessage(), $exception->getTraceAsString()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return [$ordersForUpload, $errorOrders];
|
||||
}
|
||||
|
||||
/**
|
||||
* Send payment in CRM
|
||||
*
|
||||
|
|
|
@ -83,14 +83,11 @@ if (class_exists('WC_Retailcrm_Uploader') === false) {
|
|||
*/
|
||||
public function uploadArchiveOrders(?int $page, array $ids = [])
|
||||
{
|
||||
WC_Retailcrm_Logger::info(__METHOD__, 'Archive order IDs: ' . implode(', ', $ids));
|
||||
|
||||
if (!$this->retailcrm instanceof WC_Retailcrm_Proxy) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$orderIds = [];
|
||||
$uploadErrors = [];
|
||||
|
||||
if (null !== $page) {
|
||||
$orderIds = $this->getCmsOrders($page);
|
||||
|
@ -98,14 +95,40 @@ if (class_exists('WC_Retailcrm_Uploader') === false) {
|
|||
$orderIds = $ids;
|
||||
}
|
||||
|
||||
if ($orderIds !== []) {
|
||||
foreach ($orderIds as $orderId) {
|
||||
$errorMessage = $this->orders->orderCreate($orderId);
|
||||
if ($orderIds === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (is_string($errorMessage)) {
|
||||
$uploadErrors[$orderId] = $errorMessage;
|
||||
}
|
||||
WC_Retailcrm_Logger::info(__METHOD__, 'Archive order IDs: ' . implode(', ', $ids));
|
||||
|
||||
[$ordersForUpload, $uploadErrors] = $this->orders->processOrderForUpload($orderIds);
|
||||
|
||||
try {
|
||||
$response = $this->retailcrm->ordersUpload($ordersForUpload);
|
||||
|
||||
if (!$response->isSuccessful()) {
|
||||
throw new RuntimeException(
|
||||
sprintf(
|
||||
'Failure to upload orders: %s. Status code: %s',
|
||||
$response->getErrorString(),
|
||||
$response->getStatusCode()
|
||||
)
|
||||
);
|
||||
}
|
||||
} catch (Exception $exception) {
|
||||
WC_Retailcrm_Logger::error(
|
||||
__METHOD__,
|
||||
sprintf("Error while uploading orders: %s", $exception->getMessage())
|
||||
);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/** WP version >= 6 */
|
||||
if (function_exists('wp_cache_flush_runtime')) {
|
||||
wp_cache_flush_runtime();
|
||||
} else {
|
||||
wp_cache_flush();
|
||||
}
|
||||
|
||||
$this->logOrdersUploadErrors($uploadErrors);
|
||||
|
|
Loading…
Add table
Reference in a new issue