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?

21

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.

|improve this answer|||||
  • 3
    i used this code.$email = Mage::getSingleton('customer/session')->getCustomer()->getEmail(); $subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email); $status = $subscriber->isSubscribed(); – ravisoni Apr 29 '13 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 '13 at 9:08
  • @ravisoni great answer!!! :) – jruzafa Mar 31 '14 at 8:24
  • can you tell me is_subscribed is in which table save or is it save in session – ND17 Jul 13 '15 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. – Darren Felton Jan 20 '16 at 19:04
6

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;
        }
    }
|improve this answer|||||
  • Your answer should be the accepted one, the other ones do not care if customer is unsubscribed or pending – Alex Jul 27 '18 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();
    }
}
|improve this answer|||||
  • i have added this code in observer.php file but not get anything about subscribe or not. – Sarfaraj Sipai Oct 3 '18 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.

|improve this answer|||||
  • 1
    if you use isSubscribed() then it not require to use $subscriberModel->getId() – Amit Bera Sep 29 '15 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.