From fe64c3a3f92450a5026d19261c4e4abdc8d035ea Mon Sep 17 00:00:00 2001 From: Ivan Chaplygin Date: Tue, 4 Feb 2025 16:59:35 +0300 Subject: [PATCH] Optimization of order unloading. --- src/include/class-wc-retailcrm-orders.php | 26 +++++++++++++ src/include/class-wc-retailcrm-uploader.php | 41 ++++++++++++++++----- 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/src/include/class-wc-retailcrm-orders.php b/src/include/class-wc-retailcrm-orders.php index 596b640..33124e2 100644 --- a/src/include/class-wc-retailcrm-orders.php +++ b/src/include/class-wc-retailcrm-orders.php @@ -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 * diff --git a/src/include/class-wc-retailcrm-uploader.php b/src/include/class-wc-retailcrm-uploader.php index 2422d27..eb8c909 100644 --- a/src/include/class-wc-retailcrm-uploader.php +++ b/src/include/class-wc-retailcrm-uploader.php @@ -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);