From 54959b7a15801a32802adfc7b78758c1296f7237 Mon Sep 17 00:00:00 2001 From: opheugene Date: Thu, 19 Nov 2020 12:29:30 +0300 Subject: [PATCH] Fixed resetting customer's fields in Prestashop in case of it deleting in RetailCRM --- retailcrm/lib/RetailcrmHistory.php | 43 +++++++++++++----------- retailcrm/lib/RetailcrmHistoryHelper.php | 1 - tests/lib/RetailcrmHistoryTest.php | 13 +++++++ 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/retailcrm/lib/RetailcrmHistory.php b/retailcrm/lib/RetailcrmHistory.php index ca6fef6..879629b 100644 --- a/retailcrm/lib/RetailcrmHistory.php +++ b/retailcrm/lib/RetailcrmHistory.php @@ -87,34 +87,39 @@ class RetailcrmHistory $customerBuilder->setDataCrm($customerHistory); if (isset($customerHistory['externalId'])) { + $customerData = self::$api->customersGet($customerHistory['externalId'], 'externalId'); - $foundCustomer = new Customer($customerHistory['externalId']); - $customerAddress = new Address(RetailcrmTools::searchIndividualAddress($foundCustomer)); - $addressBuilder = new RetailcrmCustomerAddressBuilder(); + if (isset($customerData['customer']) && $customerData['customer']) { - $addressBuilder - ->setCustomerAddress($customerAddress); + $foundCustomer = new Customer($customerHistory['externalId']); + $customerAddress = new Address(RetailcrmTools::searchIndividualAddress($foundCustomer)); + $addressBuilder = new RetailcrmCustomerAddressBuilder(); - $customerBuilder - ->setCustomer($foundCustomer) - ->setAddressBuilder($addressBuilder) - ->setDataCrm($customerData['customer']) - ->build(); + $addressBuilder + ->setCustomerAddress($customerAddress); - $customer = $customerBuilder->getData()->getCustomer(); - $address = $customerBuilder->getData()->getCustomerAddress(); + $customerBuilder + ->setCustomer($foundCustomer) + ->setAddressBuilder($addressBuilder) + ->setDataCrm($customerData['customer']) + ->build(); - if (self::loadInCMS($customer, 'update') === false) { - continue; - } + $customer = $customerBuilder->getData()->getCustomer(); + $address = $customerBuilder->getData()->getCustomerAddress(); - if (!empty($address)) { - RetailcrmTools::assignAddressIdsByFields($customer, $address); - - if (self::loadInCMS($address, 'update') === false) { + if (self::loadInCMS($customer, 'update') === false) { continue; } + + if (!empty($address)) { + RetailcrmTools::assignAddressIdsByFields($customer, $address); + + if (self::loadInCMS($address, 'update') === false) { + continue; + } + } } + } else { $customerBuilder->build(); diff --git a/retailcrm/lib/RetailcrmHistoryHelper.php b/retailcrm/lib/RetailcrmHistoryHelper.php index 9377957..ef18c35 100644 --- a/retailcrm/lib/RetailcrmHistoryHelper.php +++ b/retailcrm/lib/RetailcrmHistoryHelper.php @@ -171,7 +171,6 @@ class RetailcrmHistoryHelper { if (isset($change['deleted']) && $change['deleted'] - && isset($customers[$change['customer']['id']]) ) { $customers[$change['customer']['id']]['deleted'] = true; continue; diff --git a/tests/lib/RetailcrmHistoryTest.php b/tests/lib/RetailcrmHistoryTest.php index 0c885ce..40e00e9 100644 --- a/tests/lib/RetailcrmHistoryTest.php +++ b/tests/lib/RetailcrmHistoryTest.php @@ -47,6 +47,19 @@ class RetailcrmHistoryTest extends RetailcrmTestCase ) ); + $this->apiMock->expects($this->any()) + ->method('customersGet') + ->willReturn( + new RetailcrmApiResponse( + '200', + json_encode( + array( + 'customer' => $this->getApiCustomer() + ) + ) + ) + ); + RetailcrmHistory::$default_lang = (int)Configuration::get('PS_LANG_DEFAULT'); RetailcrmHistory::$api = $this->apiMock;