Skip to content

Commit dad3038

Browse files
author
mmoser
committed
Mailchimp sync improvements: don't delete email addresses which exist in other subscribed customer objects.
This helps for some edge cases.
1 parent a452f07 commit dad3038

File tree

3 files changed

+88
-4
lines changed

3 files changed

+88
-4
lines changed

src/Newsletter/ProviderHandler/Mailchimp.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
namespace CustomerManagementFrameworkBundle\Newsletter\ProviderHandler;
1717

1818
use CustomerManagementFrameworkBundle\ActivityManager\ActivityManagerInterface;
19+
use CustomerManagementFrameworkBundle\CustomerProvider\CustomerProviderInterface;
1920
use CustomerManagementFrameworkBundle\DataTransformer\Cleanup\Email;
2021
use CustomerManagementFrameworkBundle\DataTransformer\DataTransformerInterface;
2122
use CustomerManagementFrameworkBundle\Model\Activity\MailchimpStatusChangeActivity;
@@ -759,4 +760,36 @@ public function didMergeFieldDataChange($pimcoreField, $pimcoreData, $mailchimpI
759760

760761
return $this->fieldTransformers[$pimcoreField]->didMergeFieldDataChange($pimcoreData, $mailchimpImportData);
761762
}
763+
764+
/**
765+
* @param $email
766+
* @param int|false $customerId
767+
* @return bool
768+
*/
769+
public function doesOtherSubscribedCustomerWithEmailExist($email, $customerId = false)
770+
{
771+
772+
if(!$email) {
773+
return false;
774+
}
775+
/**
776+
* @var CustomerProviderInterface $customerProvider
777+
*/
778+
$customerProvider = \Pimcore::getContainer()->get(CustomerProviderInterface::class);
779+
$list = $customerProvider->getList();
780+
if($customerId) {
781+
$list->setCondition('trim(lower(email)) = ? and o_id != ?', [trim(strtolower($email)), $customerId]);
782+
} else {
783+
$list->setCondition('trim(lower(email)) = ?', [trim(strtolower($email))]);
784+
}
785+
786+
787+
foreach($list as $_customer) {
788+
if(in_array($this->getMailchimpStatus($_customer), array(self::STATUS_PENDING, self::STATUS_SUBSCRIBED))) {
789+
return true;
790+
}
791+
}
792+
793+
return false;
794+
}
762795
}

src/Newsletter/ProviderHandler/Mailchimp/CustomerExporter/BatchExporter.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,20 @@ protected function createBatchUpdateOperation(Batch $batch, CustomerInterface $c
197197
*/
198198
protected function createBatchDeleteOperation(Batch $batch, NewsletterQueueItemInterface $item, Mailchimp $mailchimpProviderHandler)
199199
{
200+
if($mailchimpProviderHandler->doesOtherSubscribedCustomerWithEmailExist($item->getEmail(), $item->getCustomerId())) {
201+
202+
$this->getLogger()->info(
203+
sprintf(
204+
'[MailChimp][CUSTOMER %s][%s] Deletion skipped as another subscribed customer with the same email exists.',
205+
$item->getCustomerId(),
206+
$mailchimpProviderHandler->getShortcut()
207+
)
208+
);
209+
210+
$item->setSuccessfullyProcessed(true);
211+
return;
212+
}
213+
200214
$exportService = $this->exportService;
201215
$apiClient = $this->apiClient;
202216

@@ -360,13 +374,16 @@ protected function processSuccessfullItem(Mailchimp $mailchimpProviderHandler, N
360374

361375
/** @var MailchimpAwareCustomerInterface|ElementInterface $customer */
362376
$customer = $item->getCustomer();
363-
$entry = $mailchimpProviderHandler->buildEntry($customer);
364-
$remoteId = $apiClient->subscriberHash($entry['email_address']);
377+
365378

366379
$operation = $item->getOverruledOperation() ?: $item->getOperation();
367380

368381
// add note
369382
if ($operation == NewsletterQueueInterface::OPERATION_UPDATE) {
383+
384+
$entry = $mailchimpProviderHandler->buildEntry($customer);
385+
$remoteId = $apiClient->subscriberHash($entry['email_address']);
386+
370387
$exportService
371388
->createExportNote($customer, $mailchimpProviderHandler->getListId(), $remoteId, null, 'Mailchimp Export [' . $mailchimpProviderHandler->getShortcut() . ']', ['exportdataMd5' => $exportService->getMd5($entry)])
372389
->save();
@@ -383,6 +400,9 @@ protected function processSuccessfullItem(Mailchimp $mailchimpProviderHandler, N
383400
]
384401
);
385402
} elseif ($customer) {
403+
$entry = $mailchimpProviderHandler->buildEntry($customer);
404+
$remoteId = $apiClient->subscriberHash($entry['email_address']);
405+
386406
$exportService
387407
->createExportNote($customer, $mailchimpProviderHandler->getListId(), $remoteId, null, 'Mailchimp Deletion [' . $mailchimpProviderHandler->getShortcut() . ']')
388408
->save();
@@ -400,9 +420,14 @@ protected function processSuccessfullItem(Mailchimp $mailchimpProviderHandler, N
400420
);
401421
}
402422

403-
$status = isset($entry['status']) ? $entry['status'] : $entry['status_if_new'];
404-
$mailchimpProviderHandler->updateMailchimpStatus($customer, $status);
423+
if($customer) {
424+
425+
$entry = $mailchimpProviderHandler->buildEntry($customer);
405426

427+
$status = isset($entry['status']) ? $entry['status'] : $entry['status_if_new'];
428+
$mailchimpProviderHandler->updateMailchimpStatus($customer, $status);
429+
430+
}
406431
$item->setSuccessfullyProcessed(true);
407432
}
408433

src/Newsletter/ProviderHandler/Mailchimp/CustomerExporter/SingleExporter.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,19 @@ protected function delete(MailchimpAwareCustomerInterface $customer, NewsletterQ
221221
$exportService = $this->exportService;
222222
$apiClient = $this->apiClient;
223223

224+
if($mailchimpProviderHandler->doesOtherSubscribedCustomerWithEmailExist($customer->getEmail(), $customer->getId())) {
225+
$this->getLogger()->debug(
226+
sprintf(
227+
'[MailChimp][CUSTOMER %s][%s] Skip deletion of customer as another subscribed customer with email %s exists.',
228+
$customer->getId(),
229+
$mailchimpProviderHandler->getShortcut(),
230+
$customer->getEmail()
231+
)
232+
);
233+
$item->setSuccessfullyProcessed(true);
234+
return;
235+
}
236+
224237
// entry to send to API
225238
$entry = $mailchimpProviderHandler->buildEntry($customer);
226239
$remoteId = $this->apiClient->subscriberHash($item->getEmail());
@@ -297,6 +310,19 @@ protected function processDeleteQueueItem(NewsletterQueueItemInterface $item, Ma
297310
$exportService = $this->exportService;
298311
$apiClient = $this->apiClient;
299312

313+
if($mailchimpProviderHandler->doesOtherSubscribedCustomerWithEmailExist($item->getEmail(), $item->getCustomerId())) {
314+
$this->getLogger()->info(
315+
sprintf(
316+
'[MailChimp][CUSTOMER %s][%s] Deletion skipped as another subscribed customer with the same email exists.',
317+
$item->getCustomerId(),
318+
$mailchimpProviderHandler->getShortcut()
319+
)
320+
);
321+
322+
$item->setSuccessfullyProcessed(true);
323+
return;
324+
}
325+
300326
// entry to send to API
301327
$remoteId = $this->apiClient->subscriberHash($item->getEmail());
302328

0 commit comments

Comments
 (0)