Updating customers save triggers to pass an array

- Customer CSV import will potentially have many customerIds to send to.
- Rework mailchimp onCustomerSaved() to receive an array of ids instead of a single ID

Signed-off-by: objec <objecttothis@gmail.com>
This commit is contained in:
objec
2026-05-20 19:41:38 +04:00
parent 4c7ac7b5d0
commit 50eead4da4
2 changed files with 22 additions and 18 deletions

View File

@@ -232,7 +232,7 @@ class Customers extends Persons
];
if ($this->customer->saveCustomer($personData, $customerData, $customerId)) {
Events::trigger('customer_saved', $customerData['person_id']);
Events::trigger('customer_saved', [$customerData['person_id']]);
// New customer
if ($customerId == NEW_ENTRY) {
@@ -287,7 +287,7 @@ class Customers extends Persons
}
/**
* This deletes customers from the customers table
* This deletes customers from the customer's table
* @return ResponseInterface
*/
public function postDelete(): ResponseInterface
@@ -354,6 +354,7 @@ class Customers extends Persons
$rowNumber = 1;
$failCodes = [];
$customerIds = [];
while (($data = fgetcsv($handle)) !== false) {
$consent = $data[3] == '' ? 0 : 1;
@@ -401,8 +402,7 @@ class Customers extends Persons
$failCodes[] = $rowNumber;
log_message('error', "Row $rowNumber was not imported: Either email or account number already exist or data was invalid.");
} elseif ($this->customer->saveCustomer($personData, $customerData)) {
Events::trigger('customer_saved', $customerData['person_id']);
$customerIds[] = $customerData['person_id'];
} else {
$failCodes[] = $rowNumber;
}
@@ -415,6 +415,8 @@ class Customers extends Persons
return $this->response->setJSON(['success' => false, 'message' => $message]);
} else {
Events::trigger('customer_saved', $customerIds);
return $this->response->setJSON(['success' => true, 'message' => lang('Customers.csv_import_success')]);
}
} else {

View File

@@ -190,29 +190,31 @@ class MailchimpPlugin extends BasePlugin
echo $this->renderView('customer_tab', $viewData);
}
public function onCustomerSaved(int $customerId): void
public function onCustomerSaved(array $customerIds): void
{
if (!$this->shouldSyncOnSave()) {
return;
}
log_message('debug', "Customer saved event received for ID: {$customerId}");
$customerModel = new Customer();
$customer = $customerModel->getInfo($customerId);
$subscriptionStatus = !empty($customer->consent)
? strtolower(SubscriptionStatus::SUBSCRIBED->name)
: strtolower(SubscriptionStatus::UNSUBSCRIBED->name);
foreach ($customerIds as $customerId) {
log_message('debug', "Customer saved event received for ID: {$customerId}");
$personData = [
'email' => $customer->email ?? '',
'first_name' => $customer->first_name ?? '',
'last_name' => $customer->last_name ?? '',
];
$customerData = ['person_id' => $customerId];
$customer = $customerModel->getInfo($customerId);
$this->mailchimpLibrary->synchronizeSubscription($personData, $customerData, $subscriptionStatus);
$subscriptionStatus = !empty($customer->consent)
? strtolower(SubscriptionStatus::SUBSCRIBED->name)
: strtolower(SubscriptionStatus::UNSUBSCRIBED->name);
$personData = [
'email' => $customer->email ?? '',
'first_name' => $customer->first_name ?? '',
'last_name' => $customer->last_name ?? '',
];
$this->mailchimpLibrary->synchronizeSubscription($personData, ['person_id' => $customerId], $subscriptionStatus);
}
}
public function onCustomerDeleted(int $customerId, string $email): void