diff --git a/app/Controllers/Customers.php b/app/Controllers/Customers.php index b3bc72304..664800cab 100644 --- a/app/Controllers/Customers.php +++ b/app/Controllers/Customers.php @@ -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 { diff --git a/app/Plugins/MailchimpPlugin/MailchimpPlugin.php b/app/Plugins/MailchimpPlugin/MailchimpPlugin.php index 3d62faf9d..e301953f3 100644 --- a/app/Plugins/MailchimpPlugin/MailchimpPlugin.php +++ b/app/Plugins/MailchimpPlugin/MailchimpPlugin.php @@ -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