Multi-store capabilities for Magento newsletter

If you use Magento multi-store feature, you possibly know that visitors of the store cannot sign up for newsletters of all multi-stores. Store visitor will be able to sign up only for one store.

Lets imagine that you have two multi-stores: one for Gifts and other for Chocolate. If some of your visitor will open Gifts store and sign up for Gifts newsletter, Magento will record it. However if the same visitor will come to Chocolate store the other day and submit his email in newsletter box, in this case Magento will remove his email from Gifts newsletter and add him to Chocolate newsletter. By default it is not possible to sign up for News of both stores.

In this article we will show how to overcome this limitation and allow your visitors to sign up for multi-store newsletters.

Creation of multi-store newsletter module for Magento

To achieve our goal, we need to create a new module.

1 - Lets create new directory for our future module, e.g.:

app/code/community/Turnkeye/Newsletter

Also we will need to create configuration file that will override newsletter/subscriber resource model. Here is the content of the config.xml:

< ?xml version="1.0"?>
<!--File: app/code/community/Turnkeye/Newsletter/etc/config.xml -->
<config>
    <modules>
        <Turnkeye_Newsletter>
            <version>0.0.1</version>
        </Turnkeye_Newsletter>
    </modules>
    <global>
        <models>
            <newsletter_mysql4> <!-- We use newsletter_mysql4 for compatibility -->
                <rewrite>
                    <subscriber>Turnkeye_Newsletter_RW_Newsletter_Model_Resource_Subscriber</subscriber>
                </rewrite>
            </newsletter_mysql4>
        </models>
    </global>
</config>

2 - Next we need to create new class: Turnkeye_Newsletter_RW_Newsletter_Model_Resource_Subscriber.

< ?php // File: app/code/community/Turnkeye/Newsletter/RW/Newsletter/Model/Resource/Subscriber.php
 /**
 * RW Resource Model
 *
 * @category   Turnkeye
 * @package    Turnkeye_Newsletter
 * @author     Kalashnikov Evgeniy
 */
class Turnkeye_Newsletter_RW_Newsletter_Model_Resource_Subscriber extends Mage_Newsletter_Model_Mysql4_Subscriber
{
    /**
     * Load subscriber from DB by email
     *
     * @param string $subscriberEmail
     * @return array
     */
    public function loadByEmail($subscriberEmail)
    {
        /** @var $customerSession Mage_Customer_Model_Session */
        $customerSession = Mage::getSingleton('customer/session');
        $ownerId = Mage::getModel('customer/customer')
            ->setWebsiteId(Mage::app()->getStore()->getWebsiteId())
            ->loadByEmail($subscriberEmail)
            ->getId();

        $storeId = $customerSession->isLoggedIn() && $ownerId == $customerSession->getId()
            ? $customerSession->getCustomer()->getStoreId()
            : Mage::app()->getStore()->getId();

        $select = $this->_read->select()
            ->from($this->getMainTable())
            ->where('subscriber_email=:subscriber_email')
            ->where('store_id=:store_id'); // Add store ID for newsletters

        $result = $this->_read->fetchRow($select, array(
            'subscriber_email'  => $subscriberEmail,
            'store_id'          => $storeId
        ));

        if (!$result) {
            return array();
        }

        return $result;
    }

    /**
     * Load subscriber by customer
     *
     * @param Mage_Customer_Model_Customer $customer
     * @return array
     */
    public function loadByCustomer(Mage_Customer_Model_Customer $customer)
    {
        $select = $this->_read->select()
            ->from($this->getMainTable())
            ->where('customer_id=:customer_id')
            ->where('store_id=:store_id');

        $result = $this->_read->fetchRow($select, array(
            'customer_id'   => $customer->getId(),
            'store_id'      => $customer->getStoreId()
        ));

        if ($result) {
            return $result;
        }

        $select = $this->_read->select()
            ->from($this->getMainTable())
            ->where('subscriber_email=:subscriber_email')
            ->where('store_id=:store_id');

        $result = $this->_read->fetchRow($select, array(
            'subscriber_email'  => $customer->getEmail(),
            'store_id'          => $customer->getStoreId()
        ));

        if ($result) {
            return $result;
        }

        return array();
    }
}

3 - The module is almost ready. The last step is to activate it.

Create new file app/etc/modules/Turnkeye_Newsletter.xml

< ?xml version="1.0"?>
<!-- File: app/etc/modules/Turnkeye_Newsletter.xml -->
<config>
    <modules>
        <Turnkeye_Newsletter>
            <active>true</active>
            <codePool>community</codePool>
            <depends>
                <Mage_Newsletter />
            </depends>
        </Turnkeye_Newsletter>
    </modules>
</config>

As usual, don't forget to reset compiler cache and Magento cache. Otherwise you will don't see the module ;)