Skip to content

Commit

Permalink
Merged PR 16800: Subscriber Status Data Field not synced
Browse files Browse the repository at this point in the history
**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
  • Loading branch information
sertlab authored and sta1r committed Oct 22, 2019
1 parent 5bea8f1 commit bb2ee3b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
2 changes: 1 addition & 1 deletion code/Dotdigitalgroup/Email/Model/Newsletter/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
59 changes: 43 additions & 16 deletions code/Dotdigitalgroup/Email/Model/Sync/Contact/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
}

0 comments on commit bb2ee3b

Please sign in to comment.