Skip to content

Commit

Permalink
ref #90809 Changed the logic of customer subscriptions to promotional…
Browse files Browse the repository at this point in the history
… newsletters (#259)
  • Loading branch information
uryvskiy-dima authored Jul 24, 2023
1 parent a375804 commit 571263c
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 22 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.1.12
4.1.13
7 changes: 7 additions & 0 deletions src/upload/admin/controller/extension/module/retailcrm.php
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,13 @@ private function addEvents()
'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,
Expand Down
14 changes: 14 additions & 0 deletions src/upload/catalog/controller/extension/module/retailcrm.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ public function customer_create($parameter1, $parameter2 = null, $parameter3 = n
}
}

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
*
Expand Down
8 changes: 6 additions & 2 deletions src/upload/system/library/retailcrm/lib/history/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ public function handleCustomer(&$customer_data, $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;
}

Expand Down Expand Up @@ -109,7 +113,7 @@ public function handleCustomFields(&$customer_data, $customer) {
}
}

$customer_data['custom_field'] = isset($custom_fields) ? $custom_fields : '';
$customer_data['custom_field'] = $custom_fields ?? [];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function __construct(\RetailcrmProxy $proxy, RetailcrmCustomerConverter $
}

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);
}
Expand All @@ -23,13 +23,22 @@ public function editCustomer($customer_data, $address) {
$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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

class RetailcrmCustomerConverter {
protected $data;
protected $customer_data = array();
protected $address = array();

protected $customer_data = [];
protected $address = [];
protected $isSubscribed;
protected $settingsManager;

public function __construct(
Expand All @@ -19,10 +19,11 @@ public function getCustomer() {
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;
}
Expand All @@ -34,26 +35,26 @@ public function setCustomerData() {
$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;
}

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;
Expand Down
1 change: 1 addition & 0 deletions src/upload/system/library/retailcrm/objects.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<field id="cumulative_discount" group="customer">cumulativeDiscount</field>
<field id="personal_discount" group="customer">personalDiscount</field>
<field id="discount_card_number" group="customer">discountCardNumber</field>
<field id="email_marketing_unsubscribed_at" group="customer">emailMarketingUnsubscribedAt</field>

<field id="address.index" group="customerAddress">index</field>
<field id="address.country" group="customerAddress">country</field>
Expand Down
9 changes: 5 additions & 4 deletions tests/system/lib/service/RetailcrmCustomerConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function testSetCustomerData() {
$customer_data = $model->getCustomer(1);

$customer = $converter
->initCustomerData($customer_data, array())
->initCustomerData($customer_data, [], true)
->setCustomerData()
->getCustomer();

Expand All @@ -20,23 +20,24 @@ public function testSetCustomerData() {
$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();

Expand Down

0 comments on commit 571263c

Please sign in to comment.