0

I use this code to set the subscription status:

const STATUS_SUBSCRIBED     = 1;
const STATUS_NOT_ACTIVE     = 2;
const STATUS_UNSUBSCRIBED   = 3;
const STATUS_UNCONFIRMED    = 4;

...

$customer = Mage::getSingleton('customer/session')->getCustomer();
$customerID = $customer->getId();

$customerModel = Mage::getModel('customer/customer')
        ->setWebsiteId(Mage::app()->getStore()->getWebsiteId())
        ->load($customerID);

Mage::getModel('newsletter/subscriber')
        ->setCustomerId($customerModel->getId())
        ->setStatus(self::STATUS_SUBSCRIBED)
        ->setEmail("myemail@foo.com")
        ->save();

But the state is not saved and the checkbox* is not getting set in the backend. What am I doing wrong?

*the checkbox I talk about is at: Customers -> manage customers -> 'enter a customer account' -> Newsletter

1

Below code worked for me --

in Guest case--

$email = "test@gmail.com";

$subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
$subscriber->setStoreId(Mage::app()->getStore()->getId());
$subscriber->setSubscriberStatus(Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED);
$subscriber->setEmail($email);
$subscriber->setCustomerId(0);
$subscriber->save();

in case of customer email -

$email = "test@gmail.com";
$customerID = 1;

$subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
$subscriber->setStoreId(Mage::app()->getStore()->getId());
$subscriber->setSubscriberStatus(Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED);
$subscriber->setEmail($email);
$subscriber->setCustomerId($customerID);
$subscriber->save();
  • Thanks so far, we are almost there!! It does work if I use STATUS_SUBSCRIBED, but not if I use STATUS_UNSUBSCRIBED ._. how can I unsubscribe a user? – Black Oct 4 '18 at 18:10
  • it works well for unsubscribed case with Mage_Newsletter_Model_Subscriber::STATUS_UNSUBSCRIBED – Shashank Kumrawat Oct 5 '18 at 5:22
  • Nope, I tried it. Won't work. The checkbox is still set. – Black Oct 5 '18 at 7:19
  • can you paste your code for unsubscribed ? – Shashank Kumrawat Oct 5 '18 at 7:21
  • It seems like I strictly have to write $subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email); like you did, then it works! If I put loadByEmail($email) in a separate line e.g. $subscriber->loadByEmail($email); then it does not work. That was my culprit. But it does not work if I use another email adress then the one used to register the account. – Black Oct 5 '18 at 11:52
0

Try to use this below code :

$subscriberModel = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
$subscriberModel->setCustomerId($customerModel->getId())
            ->setSubscriberStatus(self::STATUS_SUBSCRIBED)
            ->setEmail("myemail@foo.com")
            ->save();
  • I already tried it setStatus is just an alias for setSubscriberStatus. Makes no difference. – Black Oct 4 '18 at 9:33
  • Is it for new record or existing record? – Rohan Hapani Oct 4 '18 at 9:35
  • Existing Record. But I want it to work with both. – Black Oct 4 '18 at 9:39
  • Then, you need to first load record. – Rohan Hapani Oct 4 '18 at 9:40
  • Check my updated answer. – Rohan Hapani Oct 4 '18 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.