From f129f8706faa852ff2f4786404f99ad09dde5922 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 13 Feb 2020 15:17:52 +0300 Subject: [PATCH] fix customer squashing --- src/include/class-wc-retailcrm-customers.php | 65 +++++++++++++++++++- src/include/class-wc-retailcrm-orders.php | 4 ++ 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/src/include/class-wc-retailcrm-customers.php b/src/include/class-wc-retailcrm-customers.php index c623bc6..57a219a 100644 --- a/src/include/class-wc-retailcrm-customers.php +++ b/src/include/class-wc-retailcrm-customers.php @@ -383,13 +383,23 @@ if (!class_exists('WC_Retailcrm_Customers')) : $search = $this->retailcrm->customersList(array('email' => $filter['email'])); } - if ($search->isSuccessful()) { + if (isset($search) && $search->isSuccessful()) { + $customer = false; + if (isset($search['customers'])) { if (empty($search['customers'])) { return false; } - $customer = reset($search['customers']); + if (isset($filter['email']) && count($filter) == 1) { + foreach ($search['customers'] as $finding) { + if (isset($finding['email']) && $finding['email'] == $filter['email']) { + $customer = $finding; + } + } + } else { + $customer = reset($search['customers']); + } } else { $customer = !empty($search['customer']) ? $search['customer'] : false; } @@ -410,7 +420,11 @@ if (!class_exists('WC_Retailcrm_Customers')) : */ public function findCustomerEmailOrId($customerExternalId, $customerEmailOrPhone) { - $customer = $this->searchCustomer(array('externalId' => $customerExternalId)); + $customer = false; + + if (!empty($customerExternalId)) { + $customer = $this->searchCustomer(array('externalId' => $customerExternalId)); + } if (!$customer) { $customer = $this->searchCustomer(array('email' => $customerEmailOrPhone)); @@ -460,6 +474,51 @@ if (!class_exists('WC_Retailcrm_Customers')) : return false; } + /** + * Find corporate customer by main company name + * + * @param $companyName + * + * @return array + *TODO + * Replace with filter[nickName][] (search by nickname, company name and VAT). + * Logic below is slow and only can search by main company. + */ + public function findCorporateCustomerByMainCompany($companyName) + { + $response = true; + $page = null; + + do { + $response = $this->retailcrm->customersCorporateList(array(), $page, 100); + + if ($response instanceof WC_Retailcrm_Response && $response->isSuccessful()) { + if (is_null($page) && isset($response['pagination']['totalPageCount'])) { + $page = $response['pagination']['totalPageCount']; + } + + if ($response->offsetExists('customersCorporate')) { + foreach ($response['customersCorporate'] as $crmCorporate) { + if (isset($crmCorporate['mainCompany']) + && $crmCorporate['mainCompany']['name'] == $companyName + ) { + return $crmCorporate; + } + } + } + + $page--; + } + + time_nanosleep(0, 300000000); + } while ($response instanceof WC_Retailcrm_Response && + $response->isSuccessful() && + (isset($response['pagination']) && $page > 0) + ); + + return array(); + } + /** * @param WC_Order $order * diff --git a/src/include/class-wc-retailcrm-orders.php b/src/include/class-wc-retailcrm-orders.php index 6398c6d..963a5f9 100644 --- a/src/include/class-wc-retailcrm-orders.php +++ b/src/include/class-wc-retailcrm-orders.php @@ -241,6 +241,10 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : break; } } + } else { + $crmCorporate = $this + ->customers + ->findCorporateCustomerByMainCompany($wcOrder->get_billing_company()); } if (empty($crmCorporate) || (!empty($crmCorporate['mainCompany'])