diff --git a/CHANGELOG.md b/CHANGELOG.md index 55b1631..a9690a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 2022-04-25 4.6.1 +* The algorithm for getting the history of orders and customers has been optimized + ## 2022-03-17 4.6.0 * Added functionality of abandoned carts diff --git a/VERSION b/VERSION index 28446a5..f4fa8fc 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -4.6.0 \ No newline at end of file +4.6.1 \ No newline at end of file diff --git a/src/include/api/class-wc-retailcrm-client-v5.php b/src/include/api/class-wc-retailcrm-client-v5.php index fb4e897..449125f 100644 --- a/src/include/api/class-wc-retailcrm-client-v5.php +++ b/src/include/api/class-wc-retailcrm-client-v5.php @@ -245,23 +245,16 @@ class WC_Retailcrm_Client_V5 * Get corporate customers history * * @param array $filter - * @param int $page * @param int $limit * * @return WC_Retailcrm_Response */ - public function customersCorporateHistory(array $filter = [], int $page = 1, int $limit = 100) + public function customersCorporateHistory(array $filter = [], int $limit = 100) { - $parameters = [ - 'page' => $page, - 'limit' => $limit, - 'filter' => $filter, - ]; - return $this->client->makeRequest( '/customers-corporate/history', WC_Retailcrm_Request::METHOD_GET, - $parameters + ['limit' => $limit, 'filter' => $filter] ); } @@ -1306,23 +1299,16 @@ class WC_Retailcrm_Client_V5 * Get orders history * * @param array $filter - * @param int $page * @param int $limit * * @return WC_Retailcrm_Response */ - public function ordersHistory(array $filter = [], int $page = 1, int $limit = 100) + public function ordersHistory(array $filter = [], int $limit = 100) { - $parameters = [ - 'page' => $page, - 'limit' => $limit, - 'filter' => $filter, - ]; - return $this->client->makeRequest( '/orders/history', WC_Retailcrm_Request::METHOD_GET, - $parameters + ['limit' => $limit, 'filter' => $filter] ); } @@ -1625,23 +1611,16 @@ class WC_Retailcrm_Client_V5 * Get customers history * * @param array $filter - * @param int $page * @param int $limit * * @return WC_Retailcrm_Response */ - public function customersHistory(array $filter = [], int $page = 1, int $limit = 100) + public function customersHistory(array $filter = [], int $limit = 100) { - $parameters = [ - 'page' => $page, - 'limit' => $limit, - 'filter' => $filter, - ]; - return $this->client->makeRequest( '/customers/history', WC_Retailcrm_Request::METHOD_GET, - $parameters + ['limit' => $limit, 'filter' => $filter] ); } @@ -1819,7 +1798,6 @@ class WC_Retailcrm_Client_V5 * Get orders assembly history * * @param array $filter (default: array()) - * @param int $page (default: int) * @param int $limit (default: null) * * @return WC_Retailcrm_Response @@ -1828,18 +1806,12 @@ class WC_Retailcrm_Client_V5 * * @throws InvalidArgumentException */ - public function ordersPacksHistory(array $filter = [], int $page = 1, int $limit = 100) + public function ordersPacksHistory(array $filter = [], int $limit = 100) { - $parameters = [ - 'page' => $page, - 'limit' => $limit, - 'filter' => $filter, - ]; - return $this->client->makeRequest( '/orders/packs/history', WC_Retailcrm_Request::METHOD_GET, - $parameters + ['limit' => $limit, 'filter' => $filter] ); } diff --git a/src/include/class-wc-retailcrm-history.php b/src/include/class-wc-retailcrm-history.php index 767dfcb..50308d5 100644 --- a/src/include/class-wc-retailcrm-history.php +++ b/src/include/class-wc-retailcrm-history.php @@ -94,31 +94,22 @@ if (!class_exists('WC_Retailcrm_History')) : */ protected function customersHistory() { - - $sinceId = get_option('retailcrm_customers_history_since_id'); - $pagination = 1; - + $page = 1; + $sinceId = get_option('retailcrm_customers_history_since_id'); $filter = !empty($sinceId) ? ['sinceId' => $sinceId] : ['startDate' => $this->startDate->format('Y-m-d H:i:s')]; do { $historyResponse = $this->retailcrm->customersHistory($filter); - $isLastPage = $this->checkTotalPage($pagination, $historyResponse); - - if ($isLastPage) { - break; - } - $history = $this->getHistoryData($historyResponse); if (!empty($history)) { $lastChange = end($history); + $filter['sinceId'] = $lastChange['id']; update_option('retailcrm_customers_history_since_id', $lastChange['id']); - $filter['sinceId'] = $lastChange['id']; - WC_Retailcrm_Logger::debug(__METHOD__, [ 'Processing customers history, ID:', $filter['sinceId'] @@ -197,8 +188,13 @@ if (!class_exists('WC_Retailcrm_History')) : break; } - $pagination++; - } while ($pagination !== self::PAGE_LIMIT); + $page++; + + if ($page > self::PAGE_LIMIT) { + break; + } + + } while ($historyResponse['pagination']['currentPage'] < $historyResponse['pagination']['totalPageCount']); } /** @@ -208,31 +204,23 @@ if (!class_exists('WC_Retailcrm_History')) : */ protected function ordersHistory() { - $options = array_flip(array_filter($this->retailcrmSettings)); - $sinceId = get_option('retailcrm_orders_history_since_id'); - $pagination = 1; - + $page = 1; + $options = array_flip(array_filter($this->retailcrmSettings)); + $sinceId = get_option('retailcrm_orders_history_since_id'); $filter = !empty($sinceId) ? ['sinceId' => $sinceId] : ['startDate' => $this->startDate->format('Y-m-d H:i:s')]; do { $historyResponse = $this->retailcrm->OrdersHistory($filter); - $isLastPage = $this->checkTotalPage($pagination, $historyResponse); - - if ($isLastPage) { - break; - } - $history = $this->getHistoryData($historyResponse); if (!empty($history)) { $lastChange = end($history); + $filter['sinceId'] = $lastChange['id']; update_option('retailcrm_orders_history_since_id', $lastChange['id']); - $filter['sinceId'] = $lastChange['id']; - WC_Retailcrm_Logger::debug(__METHOD__, [ 'Processing orders history, ID:', $filter['sinceId'] @@ -307,8 +295,11 @@ if (!class_exists('WC_Retailcrm_History')) : break; } - $pagination++; - } while ($pagination !== self::PAGE_LIMIT); + if ($page > self::PAGE_LIMIT) { + break; + } + + } while ($historyResponse['pagination']['currentPage'] < $historyResponse['pagination']['totalPageCount']); WC_Retailcrm_Plugin::$history_run = false; @@ -1449,7 +1440,12 @@ if (!class_exists('WC_Retailcrm_History')) : */ private function getHistoryData($historyResponse) { - if (!$historyResponse->isSuccessful() || empty($historyResponse['history'])) { + if ( + !$historyResponse instanceof WC_Retailcrm_Response + || !$historyResponse->isSuccessful() + || empty($historyResponse['history']) + || empty($historyResponse['pagination']) + ) { return []; } @@ -1457,23 +1453,6 @@ if (!class_exists('WC_Retailcrm_History')) : return $historyResponse['history']; } - - /** - * @param int $currentPage - * @param WC_Retailcrm_Response $historyResponse Responce from CRM - * - * @return bool - */ - private function checkTotalPage($currentPage, $historyResponse): bool - { - $totalPageCount = $historyResponse['pagination']['totalPageCount'] ?? null; - - if (empty($totalPageCount)) { - return true; - } - - return $currentPage > $totalPageCount; - } } endif; diff --git a/src/readme.txt b/src/readme.txt index 1425873..3ac93bc 100644 --- a/src/readme.txt +++ b/src/readme.txt @@ -5,7 +5,7 @@ Tags: Интеграция, Simla.com, simla Requires PHP: 7.0 Requires at least: 5.3 Tested up to: 6.0 -Stable tag: 4.6.0 +Stable tag: 4.6.1 License: GPLv1 or later License URI: http://www.gnu.org/licenses/gpl-1.0.html @@ -82,6 +82,9 @@ Asegúrate de tener una clave API específica para cada tienda. Las siguientes i == Changelog == += 4.6.1 = +* The algorithm for getting the history of orders and customers has been optimized + = 4.6.0 = * Added functionality of abandoned carts diff --git a/src/retailcrm.php b/src/retailcrm.php index ea6411e..1446be9 100644 --- a/src/retailcrm.php +++ b/src/retailcrm.php @@ -5,7 +5,7 @@ * Description: Integration plugin for WooCommerce & Simla.com * Author: RetailDriver LLC * Author URI: http://retailcrm.pro/ - * Version: 4.6.0 + * Version: 4.6.1 * Tested up to: 6.0 * WC requires at least: 5.4 * WC tested up to: 6.9 diff --git a/src/uninstall.php b/src/uninstall.php index ef2728c..d5666ca 100644 --- a/src/uninstall.php +++ b/src/uninstall.php @@ -16,7 +16,7 @@ * * @link https://wordpress.org/plugins/woo-retailcrm/ * - * @version 4.6.0 + * @version 4.6.1 * * @package RetailCRM */