From 1481edae955fa824c1a71b71ae27f30a58ca3c50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B0=D0=B2=D0=B5=D0=BB?= Date: Mon, 13 Apr 2020 15:58:56 +0300 Subject: [PATCH] fix error with client duplication --- CHANGELOG.md | 3 +++ VERSION | 2 +- src/include/class-wc-retailcrm-base.php | 27 ++++++++++++++++---- src/include/class-wc-retailcrm-customers.php | 4 +-- src/include/class-wc-retailcrm-orders.php | 18 +++++++++++-- src/readme.txt | 3 +++ src/retailcrm.php | 2 +- src/uninstall.php | 2 +- 8 files changed, 49 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e374e27..ba4af4f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 2020-04-13 3.6.2 +* Исправлена ошибка, которая приводила к дублированию некоторых клиентов + ## 2019-03-31 3.6.1 * Исправлена ошибка генерации каталога товаров diff --git a/VERSION b/VERSION index 9575d51..b727628 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.6.1 +3.6.2 diff --git a/src/include/class-wc-retailcrm-base.php b/src/include/class-wc-retailcrm-base.php index 3df57db..4925aa8 100644 --- a/src/include/class-wc-retailcrm-base.php +++ b/src/include/class-wc-retailcrm-base.php @@ -228,16 +228,33 @@ if (!class_exists('WC_Retailcrm_Base')) { update_option(static::$option_key, $options); } - /** - * Create customer in retailCRM - * @param int $customer_id - */ + /** + * Create customer in retailCRM + * + * @param int $customer_id + * + * @return void + * @throws \Exception + */ public function create_customer($customer_id) { if (WC_Retailcrm_Plugin::history_running() === true) { return; } + $client = $this->getApiClient(); + + if (empty($client)) { + return; + } + + $wcCustomer = new WC_Customer($customer_id); + $resp = $client->customersList(array('email' => $wcCustomer->get_billing_email())); + + if ($resp && $resp->isSuccessful() && isset($resp['customers']) && count($resp['customers']) > 0) { + return; + } + $this->customers->createCustomer($customer_id); } @@ -321,7 +338,7 @@ if (!class_exists('WC_Retailcrm_Base')) { /** * Get retailcrm api client * - * @return bool|WC_Retailcrm_Proxy + * @return bool|WC_Retailcrm_Proxy|\WC_Retailcrm_Client_V4|\WC_Retailcrm_Client_V5 */ public function getApiClient() { diff --git a/src/include/class-wc-retailcrm-customers.php b/src/include/class-wc-retailcrm-customers.php index a7a180b..263e59a 100644 --- a/src/include/class-wc-retailcrm-customers.php +++ b/src/include/class-wc-retailcrm-customers.php @@ -16,7 +16,7 @@ if (!class_exists('WC_Retailcrm_Customers')) : const CUSTOMER_ROLE = 'customer'; - /** @var bool | WC_Retailcrm_Proxy */ + /** @var bool | WC_Retailcrm_Proxy | \WC_Retailcrm_Client_V5 */ protected $retailcrm; /** @var array */ @@ -148,7 +148,7 @@ if (!class_exists('WC_Retailcrm_Customers')) : 'createdAt' => $createdAt->date('Y-m-d H:i:s'), 'firstName' => $firstName ? $firstName : $customer->get_username(), 'lastName' => $customer->get_last_name(), - 'email' => $customer->get_email(), + 'email' => $customer->get_billing_email(), 'address' => $this->customer_address->build($customer)->get_data() ); diff --git a/src/include/class-wc-retailcrm-orders.php b/src/include/class-wc-retailcrm-orders.php index 992a44d..3c242c7 100644 --- a/src/include/class-wc-retailcrm-orders.php +++ b/src/include/class-wc-retailcrm-orders.php @@ -135,6 +135,12 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : 'externalId' => $wpUserId )); + if (empty($foundCustomer)) { + $foundCustomer = $this->customers->searchCustomer(array( + 'email' => $wcOrder->get_billing_email() + )); + } + if (empty($foundCustomer)) { $customerId = $this->customers->createCustomer($wpUserId); @@ -142,7 +148,11 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : $this->order['customer']['id'] = $customerId; } } else { - $this->order['customer']['externalId'] = $foundCustomer['externalId']; + if (!empty($foundCustomer['externalId'])) { + $this->order['customer']['externalId'] = $foundCustomer['externalId']; + } else { + $this->order['customer']['id'] = $foundCustomer['id']; + } } } else { $foundCustomer = $this->customers->searchCustomer(array( @@ -157,7 +167,11 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : $this->order['customer']['id'] = $customerId; } } else { - $this->order['customer']['externalId'] = $foundCustomer['externalId']; + if (!empty($foundCustomer['externalId'])) { + $this->order['customer']['externalId'] = $foundCustomer['externalId']; + } else { + $this->order['customer']['id'] = $foundCustomer['id']; + } } } diff --git a/src/readme.txt b/src/readme.txt index edfd5d0..622d1f3 100644 --- a/src/readme.txt +++ b/src/readme.txt @@ -45,6 +45,9 @@ API-ключ должен быть для отдельного магазина 2. В появившихся списках справочников настройте соответствие способов доставки и оплаты, а так же статусов заказов. Отметьте галочку "Выгружать остатки", если хотите выгружать остатки из Retailcrm в магазин (подробнее смотрите в описании). == Changelog == += 3.6.2 = +* Исправлена ошибка, которая приводила к дублированию некоторых клиентов + = 3.6.1 = * Исправлена ошибка генерации каталога товаров diff --git a/src/retailcrm.php b/src/retailcrm.php index 03e49b6..a2e8a10 100644 --- a/src/retailcrm.php +++ b/src/retailcrm.php @@ -1,6 +1,6 @@