14

I need to check if the customer is subscribed to news letter or not. Currently I am using this code and it is returning nothing:

if (Mage::getSingleton('customer/session')->isLoggedIn()) {
    $status = Mage::getSingleton('customer/session')->getCustomer()->getIsSubscribed();
    echo $status;
    die();
}

This is not giving anything. Any idea how to get this to work?

4 Answers 4

22

Alternatively you can try this, if you have the customer's email address:

$subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
if ($subscriber->getId()) {
     // put your logic here...
}

Or if you have customer ID then you can directly check in newsletter_subscriber table to check if customer ID exists or not.

7
  • 3
    i used this code.$email = Mage::getSingleton('customer/session')->getCustomer()->getEmail(); $subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email); $status = $subscriber->isSubscribed();
    – Ravi Soni
    Apr 29, 2013 at 12:47
  • how could I check whether a guest customer (NOT LOGGED IN customer) has subscribed for nesletter, in the same customer session.
    – Shathish
    Jun 20, 2013 at 9:08
  • @ravisoni great answer!!! :)
    – jruzafa
    Mar 31, 2014 at 8:24
  • can you tell me is_subscribed is in which table save or is it save in session
    – ND17
    Jul 13, 2015 at 5:59
  • @ND17 the status of a newsletter subscriber is stored in `newsletter_subscriber`.`subscriber_status`. Note that the table name may have a prefix depending on your configuration. Jan 20, 2016 at 19:04
7

You have to check also the subscription status:

  if(Mage::getSingleton('customer/session')->isLoggedIn()){
        $email = Mage::getSingleton('customer/session')->getCustomer()->getData('email');
        $subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
        if($subscriber->getId())
        {
            $isSubscribed = $subscriber->getData('subscriber_status') == Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED;
        }
    }
1
  • Your answer should be the accepted one, the other ones do not care if customer is unsubscribed or pending
    – Alex
    Jul 27, 2018 at 14:02
4

In my opinion none of the above answers are very convenient in that they either don't check if the customer is actually subscribed or don't handle the case where a subscription has not been found, so here goes:

$customerIsSubscribed = false;
$customer = Mage::getSingleton('customer/session')->getCustomer();
if ($customer) {
    $customerEmail = $customer->getEmail();
    $subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($customerEmail);
    if ($subscriber) {
        $customerIsSubscribed = $subscriber->isSubscribed();
    }
}
1
  • i have added this code in observer.php file but not get anything about subscribe or not. Oct 3, 2018 at 13:16
2

To extend @Mufaddal's answer:

$subscriberModel = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
$subbed = ($subscriberModel->isSubscribed() ? true : false);

This way you check if the subscription record is present AND if the subscription status is true.

1
  • 1
    if you use isSubscribed() then it not require to use $subscriberModel->getId()
    – Amit Bera
    Sep 29, 2015 at 17:49

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.