9

I am trying to write a module that syncs my newsletter subscribers in Magento with a external database. I need to be able to update the subscription status in Magento programmatically but I am having diffuculty getting the "setStatus" method in Magento to work. It does not throw any errors but the code does not seem to have any effect. Below is the code where I call the method:

$collection = Mage::getResourceModel('newsletter/subscriber_collection')->showStoreInfo()->showCustomerInfo();

foreach ($collection as $cust) {
    $cust->setStatus(1);
}

In theory, this should set the status of all of my subscribers to "subscribed". I could optionally change the argument sent to "setStatus" to any of the below ints for a different status.

1: Subscribed 2: Status Not Active 3: Unsubscribed

How to best change the subscriber status or get this code working?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Chuck D
  • 1,718
  • 4
  • 19
  • 26

3 Answers3

8

Here an import script:

<?php
require_once("./app/Mage.php");
Mage::app();

$subscribers = array('email1@server1.com', 'email2@server2.com');

foreach ($subscribers as $email) {
    # create new subscriber without send an confirmation email
    Mage::getModel('newsletter/subscriber')->setImportMode(true)->subscribe($email);

    # get just generated subscriber
    $subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);

    # change status to "subscribed" and save
    $subscriber->setStatus(Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED);
    $subscriber->save();
}
?>
j0k
  • 22,600
  • 28
  • 79
  • 90
panticz
  • 2,135
  • 25
  • 16
  • This is exactly what I was looking for as well. Do you run this through terminal or just build a php page and then visit the page and it will run? – thismethod Apr 28 '17 at 18:29
  • How to subscribe with first name, lastname? – Jafar Pinjar Sep 10 '18 at 06:59
  • @jafarpinjar you can't add first name and last name... this ONLY ever appears in the table IF that subscriber is also a customer, and the name is taken from the customer record. – Willster May 14 '19 at 10:01
5

It seems that newsletter subscribers are also stored elsewhere. What you are setting is just a check in the customer base for some other use.

You need to do the following for each customer as well.

Mage::getModel('newsletter/subscriber')->subscribe($email);

See this link for a complete reference.

cssyphus
  • 37,875
  • 18
  • 96
  • 111
Ozair Kafray
  • 13,351
  • 8
  • 59
  • 84
3

Thanks to the link @Ozair shared I was able to figure out what I needed to do.

I was successfully setting the status of the subscriber in the Magento subscriber object but I was not saving the object. I needed to call Magento's save method so it would call the ORM and write it to the database. All I need to do was add

$cust->save();

in the for loop. Below is the whole code snippet.

$collection = Mage::getResourceModel('newsletter/subscriber_collection')->showStoreInfo()->showCustomerInfo();

foreach ($collection as $cust) {
    $cust->setStatus(1);
    $cust->save();
}

I Hope this helps someone in the future. I needed it for a Constant Contact - Magento Synchronization extension I was making: http://www.freelunchlabs.com/store/constant-contact-and-magento-sync.html

Chuck D
  • 1,718
  • 4
  • 19
  • 26
  • Hi @Chuck D, this is exactly what I need. You didn't end of having any issues with this affecting anything else? I tested it on my local and it did exactly what it says it does. – thismethod Apr 28 '17 at 18:48
  • Hi @thismethod, Nope - no issues. If you're using any third party extensions like MailChimp or ConstantContact, they usually have observers for the save subscriber event and will sync the change of to the 3rd party... – Chuck D Apr 30 '17 at 00:35