From bb2ee3b53897c10c3cbce627a63af12a5aaa5fc2 Mon Sep 17 00:00:00 2001 From: Fanis Strezos Date: Tue, 22 Oct 2019 10:44:58 +0000 Subject: [PATCH] Merged PR 16800: Subscriber Status Data Field not synced **Repro Steps** - Set this option to NO (EC> Sync Settings > Address Book Data Mapping > Allow non-subscribed contacts to be imported) - Register and sign up to newsletter - Run Contact Sync and importer - Login to EC and confirm that the contact has SUBSCRIBER_STATUS to 'Subscribed' - Go to magento storefront then go to 'My Account' and unsubscribe - Run importer sync **Actual Result** This contact in EC still have SUBSCRIBER_STATUS = 'Subscribed' Data Field **Expected Result** This contact should have SUBSCRIBER_STATUS = 'Unsubscribed' Data Field **Notes** If a user is manually unsubscribed from Magento storefront, she will still keep her "last_subscribed_at" date. Related work items: #99216 --- .../Email/Model/Newsletter/Observer.php | 2 +- .../Email/Model/Sync/Contact/Update.php | 59 ++++++++++++++----- 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/code/Dotdigitalgroup/Email/Model/Newsletter/Observer.php b/code/Dotdigitalgroup/Email/Model/Newsletter/Observer.php index ef00123b4..c07d788d7 100755 --- a/code/Dotdigitalgroup/Email/Model/Newsletter/Observer.php +++ b/code/Dotdigitalgroup/Email/Model/Newsletter/Observer.php @@ -75,7 +75,7 @@ public function handleNewsletterSubscriberSave(Varien_Event_Observer $observer) $contactEmail->setIsSubscriber(null) ->setSubscriberStatus(Mage_Newsletter_Model_Subscriber::STATUS_UNSUBSCRIBED) - ->setLastSubscribedAt(null); + ->setLastSubscribedAt($contactEmail->getLastSubscribedAt()); //save contact $contactEmail->save(); diff --git a/code/Dotdigitalgroup/Email/Model/Sync/Contact/Update.php b/code/Dotdigitalgroup/Email/Model/Sync/Contact/Update.php index 3699f6437..8c361fd5b 100644 --- a/code/Dotdigitalgroup/Email/Model/Sync/Contact/Update.php +++ b/code/Dotdigitalgroup/Email/Model/Sync/Contact/Update.php @@ -55,22 +55,7 @@ public function processCollection($collection) $this->client->postContactsResubscribe($this->client->getContactByEmail($email)); } } elseif ($item->getImportMode() == Dotdigitalgroup_Email_Model_Importer::MODE_SUBSCRIBER_UPDATE) { - $email = $importData['email']; - $id = $importData['id']; - $result = $this->client->postContacts($email); - if (isset($result->id)) { - $contactId = $result->id; - $this->client->deleteAddressBookContact( - Mage::helper('ddg')->getSubscriberAddressBook($websiteId), $contactId - ); - } else { - $contactEmail = Mage::getModel('ddg_automation/contact')->load($id); - - if ($contactEmail->getId()) { - $contactEmail->setSuppressed('1') - ->save(); - } - } + $result = $this->handleUnsubscribe($importData, $websiteId); } if ($result) { @@ -79,4 +64,46 @@ public function processCollection($collection) } } } + + /** + * Handle a contact unsubscribe: + * - set SUBSCRIBER_STATUS data field to 'Unsubscribed' + * - if EC contact exists and has been updated, delete contact from the subscribers address book + * - if not, mark as suppressed in our table + * + * @param array $importData + * @param string $websiteId + * @return mixed + */ + protected function handleUnsubscribe($importData, $websiteId) + { + $email = $importData['email']; + $id = $importData['id']; + + $subscriberStatuses = Mage::getModel('ddg_automation/apiconnector_customer') + ->subscriberStatus; + $unsubscribedValue = $subscriberStatuses[Mage_Newsletter_Model_Subscriber::STATUS_UNSUBSCRIBED]; + + $data[] = [ + 'Key' => 'SUBSCRIBER_STATUS', + 'Value' => $unsubscribedValue + ]; + + $result = $this->client->updateContactDatafieldsByEmail($email, $data); + if (isset($result->id)) { + $contactId = $result->id; + $this->client->deleteAddressBookContact( + Mage::helper('ddg')->getSubscriberAddressBook($websiteId), $contactId + ); + } else { + $contactEmail = Mage::getModel('ddg_automation/contact')->load($id); + + if ($contactEmail->getId()) { + $contactEmail->setSuppressed('1') + ->save(); + } + } + + return $result; + } } \ No newline at end of file