Tuesday, April 6, 2021

Multi-store capabilities for Magento Newsletter

popup_form Magento seems to not to be perfect at times, though i believe its GREAT. Recently i was working on a multistore project, i launched first of its store successfully. While testing Newsletter functionality for the Second store i got to know that Magento has an issue with its multistore capabilities. I digged into it and found that each time a user subscribes newsletter from a new or different store in the same setup with same Email IDthe old entry vanishes and new entry is overwritten to it. The new entry was overwriting Store Id to newsletter table for the same email address. That means a single Email ID can be subscribed to only one store at a time and that was definitely a blunder. The solution is simple. Here you can create a new module or just overwrite Newsletter Resource Model class into your custom Namespace under local codepool. You need to override  function loadByEmail and loadByCustomer from  Mage_Newsletter_Model_Mysql4_Subscribe class.
/**
 * Resource Model
 * @author Swapnil Kene
 * FILE Namespace/Newsletter/Model/Resource/Subscriber.php
 */
class Namespace_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();
    }
}

No comments:

Post a Comment