Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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;
        }
    }

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();
    }
}

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top