Added customer search by email

This commit is contained in:
gleemand 2022-08-10 10:21:50 +03:00 committed by GitHub
parent 4098be6a13
commit c27a932f2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 56 additions and 20 deletions

View File

@ -1,5 +1,10 @@
# Customers
## Предотвращение дублирования клиентов
При создании нового клиента в Prestashop происходит поиск клиента в CRM по externalId и по email. Если клиент найден, то в CRM обновляется его адрес и `externalId`.
Для отключения поиска по email необходимо использовать [фильтр](../../3.%20Customization/Filters.md) `RetailcrmFilterFindCustomerByEmail`, который должен возвращать пустой массив `[]`.
## Адреса корпоративных клиентов
Клиент считается корпоративным, если в его `invoice address` поле `company` не пустое.

View File

@ -2,4 +2,9 @@
## Create
## Предотвращение дублирования клиентов
При создании нового заказа в Prestashop происходит поиск клиента в CRM по externalId и по email. Если клиент найден, то в CRM обновляется его адрес и `externalId`.
Для отключения поиска по email необходимо использовать [фильтр](../../3.%20Customization/Filters.md) `RetailcrmFilterFindCustomerByEmail`, который должен возвращать пустой массив `[]`.
## Update

View File

@ -25,6 +25,7 @@ There are list of available filters:
* *RetailcrmFilterOrdersHistoryCreate* - array with order info, loaded from CRM
* *RetailcrmFilterOrdersHistoryUpdate* - array with assembled history for order, loaded from CRM
* *RetailcrmFilterFindCustomerByEmail* - customer array, found by email in CRM (to prevent duplicating)
* *RetailcrmFilterSaveCustomer* - built customer object, which will be saved to CMS
* *RetailcrmFilterSaveCustomerAddress* - built customer address object, which will be saved to CMS
* *RetailcrmFilterSaveCorporateCustomer* - built corporate customer object, which will be saved to CMS

View File

@ -251,7 +251,7 @@ class RetailcrmOrderBuilder
*
* @return array|bool
*/
private function createCustomerIfNotExist()
public function createCustomerIfNotExist()
{
$this->validateCmsCustomer();
@ -271,12 +271,24 @@ class RetailcrmOrderBuilder
$customer = $this->createdCustomer;
} else {
$crmCustomer = RetailcrmTools::mergeCustomerAddress($customer, $this->buildRegularAddress());
if (!RetailcrmTools::isEqualCustomerAddress($customer, $crmCustomer)) {
if (
!RetailcrmTools::isEqualCustomerAddress($customer, $crmCustomer)
|| !isset($crmCustomer['externalId'])
|| $crmCustomer['externalId'] !== $this->cmsCustomer->id
) {
if (isset($crmCustomer['tags'])) {
unset($crmCustomer['tags']);
}
$response = $this->api->customersEdit($crmCustomer);
$by = 'externalId';
if (!isset($crmCustomer['externalId']) || $crmCustomer['externalId'] !== $this->cmsCustomer->id) {
$crmCustomer['externalId'] = $this->cmsCustomer->id;
$by = 'id';
}
$response = $this->api->customersEdit($crmCustomer, $by);
if ($response instanceof RetailcrmApiResponse && $response->isSuccessful()) {
$customer = $crmCustomer;
@ -609,7 +621,14 @@ class RetailcrmOrderBuilder
{
$this->validateCmsCustomer();
if (empty($this->cmsCustomer->id) || $this->cmsCustomer->is_guest) {
if (!empty($this->cmsCustomer->id) && !$this->cmsCustomer->is_guest) {
$customer = $this->api->customersGet($this->cmsCustomer->id);
if ($customer && $customer->isSuccessful() && $customer->offsetExists('customer')) {
return $customer['customer'];
}
}
if (!empty($this->cmsCustomer->email)) {
$customers = $this->api->customersList(['email' => $this->cmsCustomer->email]);
@ -618,17 +637,18 @@ class RetailcrmOrderBuilder
&& $customers->offsetExists('customers')
&& !empty($customers['customers'])
) {
$customers = $customers['customers'];
$customer = reset($customers['customers']);
return reset($customers);
if ($customer['email'] === $this->cmsCustomer->email) {
return RetailcrmTools::filter(
'RetailcrmFilterFindCustomerByEmail',
$customer,
[
'cmsCustomer' => $this->cmsCustomer,
]
);
}
}
} else {
$customer = $this->api->customersGet($this->cmsCustomer->id);
if ($customer && $customer->isSuccessful() && $customer->offsetExists('customer')) {
return $customer['customer'];
}
}
return [];

View File

@ -460,9 +460,14 @@ class RetailCRM extends Module
{
if ($this->api) {
$customer = $params['newCustomer'];
$customerSend = RetailcrmOrderBuilder::buildCrmCustomer($customer);
$this->api->customersCreate($customerSend);
$orderBuilder = new RetailcrmOrderBuilder();
$orderBuilder
->defaultLangFromConfiguration()
->setApi($this->api)
->setCmsCustomer($customer)
->createCustomerIfNotExist()
;
return true;
}