0

I have created a custom observer method for event "newsletter_subscriber_save_after":

    <newsletter_subscriber_save_after>
      <observers>
        <customscripts_common_controller_checkout_newsletter>
          <class>Customscripts_Common_Model_Observer</class>
          <method>controller_checkout_newsletter</method>
        </customscripts_common_controller_checkout_newsletter>
      </observers>
    </newsletter_subscriber_save_after>

Inside method's code, I check if email is subscribed to newsletter list. But the check always returns true:

$email = Mage::getSingleton('customer/session')->getCustomer()->getData('email');
$subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
if($subscriber->getId()) //this is ALWAYS TRUE
{
       //do stuff
}

Any idea about this strange issue???

3
  • Use newsletter_subscriber_save_before instead of newsletter_subscriber_save_after event. Jan 11 '18 at 12:42
  • @MohitKumarArora I tried that, but it's not solving the problem
    – zekia
    Jan 11 '18 at 12:48
  • I have confirmed that $email is not subscribed. It's really strange.
    – zekia
    Jan 11 '18 at 12:58
1

Try With Below Code,it will work for you

in config.xml

<newsletter_subscriber_save_before>
   <observers>
       <customscripts_common_controller_checkout_newsletter>
            <class>salesautoassign/observer</class>
            <method>checknewsletter</method>
       </customscripts_common_controller_checkout_newsletter>
   </observers>
</newsletter_subscriber_save_before>

In Observer.php file

public function checknewsletter(Varien_Event_Observer $observer)
    {
        $event = $observer->getEvent();
        $subscriber = $event->getDataObject();
        $data = $subscriber->getData();
        $email = $data['subscriber_email'];
        // Mage::log($email,null,'email.log');

        $storeId = Mage::app()->getStore()->getStoreId();

        $subscriberStatus = Mage::getModel('newsletter/subscriber')->setStoreId($storeId)->loadByEmail($email)->getSubscriberStatus();
        if ($subscriberStatus == 1) {
            // Email already subscribed
            //put here code to handle if customer is just subscribed...
        }
    }
4
  • I tried that. It seems that the event is triggered even if client has not checked the "Subscribe to Newsletter" checkbox. Also the "if ($data['subscriber_status'] == "1" && $statusChange == true)" part is always true. Any ideas?
    – zekia
    Jan 12 '18 at 8:36
  • did you check link:stackoverflow.com/a/16531745
    – akgola
    Jan 12 '18 at 9:39
  • Even with that code, problem is the same. Why is $statusChange == true always?
    – zekia
    Jan 12 '18 at 10:16
  • Please check updated answer,it will work now.
    – akgola
    Jan 12 '18 at 10:56
1

Instead of using

$email = Mage::getSingleton('customer/session')->getCustomer()->getData('email');
$subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);

you need to use

$storeId = Mage::app()->getStore()->getStoreId();
$subscriberStatus = Mage::getModel('newsletter/subscriber')->setStoreId($storeId)->loadByEmail($email)->getSubscriberStatus();

because in case of a guest subscription, you will not get customer session.

1
  • is it normal that $subscriberStatus = null ?
    – zekia
    Jan 26 '18 at 9:21
1

Seems out that my OneStepCheckout extension was responsible for this bug.

I'm using Amasty OneStepCheckout extension and I had to change _prepareGuestQuote() function, in app/code/local/Amasty/Scheckout/Model/Type/Onepage.php file:


1. Change the following line:

$isSubscribed = Mage::app()->getRequest()->getParam('is_subscribed', false) || Mage::getSingleton('checkout/session')->getAmscheckoutIsSubscribed();

to this one:

$isSubscribed = Mage::app()->getRequest()->getParam('is_subscribed', false);

2. Change this part of code:

if ($isSubscribed) {
    $quote = $this->getQuote();

    Mage::getModel('newsletter/subscriber')->subscribe($quote->getBillingAddress()->getEmail());

    Mage::getSingleton('checkout/session')->setAmscheckoutIsSubscribed(true);
}

to this one:

if ($isSubscribed) {
    $quote = $this->getQuote();
    $storeId = Mage::app()->getStore()->getStoreId();
    $subscriberStatus = Mage::getModel('newsletter/subscriber')->setStoreId($storeId)->loadByEmail($quote->getBillingAddress()->getEmail())->getSubscriberStatus();
    if ($subscriberStatus != 1) {
        Mage::getModel('newsletter/subscriber')->subscribe($quote->getBillingAddress()->getEmail());
        Mage::getSingleton('checkout/session')->setAmscheckoutIsSubscribed(true);
    }
} 

It's not an elegant solution, but it works. I'm just posting this , in case someboody needs it.

Not the answer you're looking for? Browse other questions tagged or ask your own question.