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