Is there a way to check whether a GUEST customer has subscribed to newsletter, in that particular session?

magento.stackexchange https://magento.stackexchange.com/questions/4954

Question

I have to hide the newsletter subscription form from customers who already subscribed.

For registered and loggedin customer i'm able to check the subscription status using the following method:

$customerSession = Mage::getSingleton("customer/session");
$email = $customerSession->getCustomer()->getEmail();
$subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
    $status = $subscriber->isSubscribed(); // status = 1 if subscribed.

But I want to hide it for guest customers also (obviously for that particular session alone).

How could I do that? Can anyone help?

Was it helpful?

Solution

Your config.xml:

<config>
    <frontend>
        <events>
            <newsletter_subscriber_save_after>
                <observers>
                    <your_module_node>
                        <class>Your_Module_Model_Observer</class>
                        <method>newsletterSubscriberSaveAfter</method>
                    </your_module_node>
                </observers>
            </newsletter_subscriber_save_after>
        </events>
    </frontend>
    /*another config nodes*/
</config>

Your Observer.php:

public function newsletterSubscriberSaveAfter($observer)
{
    $subscription = $observer->getEvent()->getSubscriber();
    if ($subscription->subscriber_email) {
        Mage::getSingleton('customer/session')->setSubscribed(true);
    }
}

Template /app/design/frontend/base/default/template/newsletter/subscribe.phtml:

<?php if (!Mage::getSingleton('customer/session')->getSubscribed()): ?>
<!--here is original html -->
<?php endif; ?>

OTHER TIPS

Override the subscriber controller Mage_Newsletter_SubscriberController and on the newAction() method, when the customer is subscribed just add a flag to the session.
Then, in the newsletter block template, just check if that session flag is set.

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