From 571263ca2c8e1278ce334d6c84e71faa2cbd7b24 Mon Sep 17 00:00:00 2001 From: Uryvskiy Dima Date: Mon, 24 Jul 2023 10:43:13 +0300 Subject: [PATCH] ref #90809 Changed the logic of customer subscriptions to promotional newsletters (#259) --- CHANGELOG.md | 3 +++ VERSION | 2 +- .../controller/extension/module/retailcrm.php | 7 ++++++ .../controller/extension/module/retailcrm.php | 14 +++++++++++ .../retailcrm/lib/history/Customer.php | 8 ++++-- .../retailcrm/lib/service/CustomerManager.php | 15 ++++++++--- .../service/RetailcrmCustomerConverter.php | 25 ++++++++++--------- .../system/library/retailcrm/objects.xml | 1 + .../RetailcrmCustomerConverterTest.php | 9 ++++--- 9 files changed, 62 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 91fd812..a6f5d19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## v4.1.13 +* Changed the logic of customer subscriptions to promotional newsletters + ## v4.1.12 * Added escaping for db query in method for getting zone diff --git a/VERSION b/VERSION index b05079e..467d093 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -4.1.12 +4.1.13 diff --git a/src/upload/admin/controller/extension/module/retailcrm.php b/src/upload/admin/controller/extension/module/retailcrm.php index 956d569..e40c7f9 100644 --- a/src/upload/admin/controller/extension/module/retailcrm.php +++ b/src/upload/admin/controller/extension/module/retailcrm.php @@ -822,6 +822,13 @@ class ControllerExtensionModuleRetailcrm extends Controller 'extension/module/retailcrm/customer_edit' ); + $this->{'model_' . $this->modelEvent} + ->addEvent( + $this->moduleTitle, + 'catalog/model/account/customer/editNewsletter/after', + 'extension/module/retailcrm/customer_edit_newsletter' + ); + $this->{'model_' . $this->modelEvent} ->addEvent( $this->moduleTitle, diff --git a/src/upload/catalog/controller/extension/module/retailcrm.php b/src/upload/catalog/controller/extension/module/retailcrm.php index d046fd0..d61b074 100644 --- a/src/upload/catalog/controller/extension/module/retailcrm.php +++ b/src/upload/catalog/controller/extension/module/retailcrm.php @@ -138,6 +138,20 @@ class ControllerExtensionModuleRetailcrm extends Controller { } } + public function customer_edit_newsletter($parameter1, $parameter2, $parameter3) + { + $customerId = $this->customer->getId(); + $customer = $this->model_account_customer->getCustomer($customerId); + + if (file_exists(DIR_APPLICATION . 'model/extension/retailcrm/custom/customer.php')) { + $this->load->model('extension/retailcrm/custom/customer'); + $this->model_extension_retailcrm_custom_customer->changeInCrm($customer, $this->retailcrmApiClient); + } else { + $customer_manager = $this->retailcrm->getCustomerManager(); + $customer_manager->editCustomerNewsLetter($customer); + } + } + /** * Update customer on event * diff --git a/src/upload/system/library/retailcrm/lib/history/Customer.php b/src/upload/system/library/retailcrm/lib/history/Customer.php index c6c786f..615d45d 100644 --- a/src/upload/system/library/retailcrm/lib/history/Customer.php +++ b/src/upload/system/library/retailcrm/lib/history/Customer.php @@ -38,10 +38,14 @@ class Customer { } $customer_data['firstname'] = $customer['firstName']; - $customer_data['lastname'] = isset($customer['lastName']) ? $customer['lastName'] : ''; + $customer_data['lastname'] = $customer['lastName'] ?? ''; $customer_data['email'] = $customer['email']; $customer_data['telephone'] = $customer['phones'] ? $customer['phones'][0]['number'] : ''; + if (!empty($customer['emailMarketingUnsubscribedAt'])) { + $customer_data['newsletter'] = 0; + } + $customer_data['affiliate'] = false; } @@ -109,7 +113,7 @@ class Customer { } } - $customer_data['custom_field'] = isset($custom_fields) ? $custom_fields : ''; + $customer_data['custom_field'] = $custom_fields ?? []; } } } diff --git a/src/upload/system/library/retailcrm/lib/service/CustomerManager.php b/src/upload/system/library/retailcrm/lib/service/CustomerManager.php index 2fdda4a..02887f0 100644 --- a/src/upload/system/library/retailcrm/lib/service/CustomerManager.php +++ b/src/upload/system/library/retailcrm/lib/service/CustomerManager.php @@ -12,7 +12,7 @@ class CustomerManager { } public function createCustomer($customer_data, $address) { - $customer = $this->prepareCustomer($customer_data, $address); + $customer = $this->prepareCustomer($customer_data, $address, !empty($customer_data['newsletter'])); $this->api->customersCreate($customer); } @@ -23,13 +23,22 @@ class CustomerManager { $this->api->customersEdit($customer); } + public function editCustomerNewsLetter($customer_data) { + $this->api->customersEdit( + [ + 'externalId' => $customer_data['customer_id'], + 'subscribed' => !empty($customer_data['newsletter']), + ] + ); + } + public function uploadCustomers($customers) { $this->api->customersUpload($customers); } - public function prepareCustomer($customer_data, $address) { + public function prepareCustomer($customer_data, $address, $isSubscribed = null) { return $this->customer_converter - ->initCustomerData($customer_data, $address) + ->initCustomerData($customer_data, $address, $isSubscribed) ->setCustomerData() ->setAddress() ->setCustomFields() diff --git a/src/upload/system/library/retailcrm/lib/service/RetailcrmCustomerConverter.php b/src/upload/system/library/retailcrm/lib/service/RetailcrmCustomerConverter.php index d1cd1f6..f2b9194 100644 --- a/src/upload/system/library/retailcrm/lib/service/RetailcrmCustomerConverter.php +++ b/src/upload/system/library/retailcrm/lib/service/RetailcrmCustomerConverter.php @@ -4,9 +4,9 @@ namespace retailcrm\service; class RetailcrmCustomerConverter { protected $data; - protected $customer_data = array(); - protected $address = array(); - + protected $customer_data = []; + protected $address = []; + protected $isSubscribed; protected $settingsManager; public function __construct( @@ -19,10 +19,11 @@ class RetailcrmCustomerConverter { return $this->data; } - public function initCustomerData($customer_data, $address) { - $this->data = array(); + public function initCustomerData($customer_data, $address, $isSubscribed) { + $this->data = []; $this->customer_data = $customer_data; $this->address = $address; + $this->isSubscribed = $isSubscribed; return $this; } @@ -34,12 +35,12 @@ class RetailcrmCustomerConverter { $this->data['email'] = $this->customer_data['email']; $this->data['createdAt'] = $this->customer_data['date_added']; + if ($this->isSubscribed !== null) { + $this->data['subscribed'] = $this->isSubscribed; + } + if (!empty($this->customer_data['telephone'])) { - $this->data['phones'] = array( - array( - 'number' => $this->customer_data['telephone'] - ) - ); + $this->data['phones'] = [['number' => $this->customer_data['telephone']]]; } return $this; @@ -47,13 +48,13 @@ class RetailcrmCustomerConverter { public function setAddress() { if (!empty($this->address)) { - $this->data['address'] = array( + $this->data['address'] = [ 'index' => $this->address['postcode'], 'countryIso' => $this->address['iso_code_2'], 'region' => $this->address['zone'], 'city' => $this->address['city'], 'text' => $this->address['address_1'] . ' ' . $this->address['address_2'] - ); + ]; } return $this; diff --git a/src/upload/system/library/retailcrm/objects.xml b/src/upload/system/library/retailcrm/objects.xml index a3faa5a..df81875 100644 --- a/src/upload/system/library/retailcrm/objects.xml +++ b/src/upload/system/library/retailcrm/objects.xml @@ -14,6 +14,7 @@ cumulativeDiscount personalDiscount discountCardNumber + emailMarketingUnsubscribedAt index country diff --git a/tests/system/lib/service/RetailcrmCustomerConverterTest.php b/tests/system/lib/service/RetailcrmCustomerConverterTest.php index 52ed059..5b0636d 100644 --- a/tests/system/lib/service/RetailcrmCustomerConverterTest.php +++ b/tests/system/lib/service/RetailcrmCustomerConverterTest.php @@ -11,7 +11,7 @@ class RetailcrmCustomerConverterTest extends TestCase { $customer_data = $model->getCustomer(1); $customer = $converter - ->initCustomerData($customer_data, array()) + ->initCustomerData($customer_data, [], true) ->setCustomerData() ->getCustomer(); @@ -20,23 +20,24 @@ class RetailcrmCustomerConverterTest extends TestCase { $this->assertEquals($customer_data['lastname'], $customer['lastName']); $this->assertEquals($customer_data['email'], $customer['email']); $this->assertEquals($customer_data['date_added'], $customer['createdAt']); + $this->assertTrue($customer['subscribed']); } public function testSetAddress() { $converter = \retailcrm\factory\CustomerConverterFactory::create(static::$registry); $model = $this->loadModel('account/customer'); $customer_data = $model->getCustomer(static::CUSTOMER_ID); - $address = array( + $address = [ 'postcode' => '111111', 'iso_code_2' => 'EN', 'zone' => 'Zone', 'city' => 'City', 'address_1' => 'Address', 'address_2' => '' - ); + ]; $customer = $converter - ->initCustomerData($customer_data, $address) + ->initCustomerData($customer_data, $address, null) ->setAddress() ->getCustomer();