Search code examples
phpmagentonewslettersubscriber

Set subscriber status in Magento programmatically


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?


Solution

  • 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