Wednesday 20 May 2015

How to Disable Default Newsletters on Magento?



Many of you are not really happy with the default newsletter available with Magento. You tend to use third party mail services to accomplish the email newsletter related tasks. Since, you are using the third party service; you should ideally disable the default newsletter that comes with Magento. Let’s see how to disable default newsletters on Magento. 

Rewrite Core Magento Model

Go to config.xml and begin with rewriting the core model

<newsletter>
    <rewrite>
        <subscriber>Example_Newsletter_Model_Newsletter_Subscriber</subscriber>
    </rewrite>
</newsletter>


When you put it within the config option, you are actually allowing the store owner to decide whether or not they want to keep the config option.

Create System.xml

Your next step is to create system.xml and paste the code given below to it

<?xml version="1.0"?>

<config>
    <tabs>
        <example_tab translate="label">
            <label>Example</label>
            <sort_order>200</sort_order>
        </example_tab>
    </tabs>

    <sections>
        <example_newsletter translate="label">
            <label>Newsletter Configuration</label>
            <tab>example_test_tab</tab>
            <frontend_type>text</frontend_type>
            <sort_order>1</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
            <groups>
                <newsletter_subscription_transactional_mails>
                    <label>Newsletter Subscription Transactional Mails</label>
                    <frontend_type>text</frontend_type>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <sort_order>120</sort_order>
                    <fields>
                        <enabled translate="label">
                            <label>Enable</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <sort_order>40</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </enabled>
                    </fields>
                </newsletter_subscription_transactional_mails>
            </groups>
        </example_newsletter>
    </sections>
</config>   

Add a Path Within Helper Class

Using the code below, you can easily add a path to the helper class

class Example_Newsletter_Helper_Data extends Mage_Core_Helper_Abstract
{
    const XML_PATH_NEWSLETTER_MAILS  = 'example_newsletter/newsletter_subscription_transactional_mails/enabled';

    public function getNewsletterSubscriptionMailEnabled()
    {
        return Mage::getStoreConfig(self::XML_PATH_NEWSLETTER_MAILS);
    }
}

The new class created for this purpose is  Example_Newsletter_Model_Newsletter_Subscriber . you need to extend this class to the core class Mage_Newsletter_Model_Subscriber

Now copy paste three methods from the core class to the new class and make small modifications accordingly

The three minor modifications include

• public function subscribe($email)
• public function subscribeCustomer($customer)
• public function unsubscribe()

Replace the code in subscribe method

if ($isConfirmNeed === true
    && $isOwnSubscribes === false
) {
    $this->sendConfirmationRequestEmail();
} else {
    $this->sendConfirmationSuccessEmail();
}
With
if ((bool) Mage::helper('example_newsletter')->getNewsletterSubscriptionMailEnabled()) {
    if ($isConfirmNeed === true && $isOwnSubscribes === false) {
        $this->sendConfirmationRequestEmail();
    } else {
        $this->sendConfirmationSuccessEmail();
    }
}
Similarly, the following code in subscribecustomer method()
if ($this->getIsStatusChanged() && $status == self::STATUS_UNSUBSCRIBED) {
    $this->sendUnsubscriptionEmail();
} elseif ($this->getIsStatusChanged() && $status == self::STATUS_SUBSCRIBED) {
    $this->sendConfirmationSuccessEmail();
}
Should be replaced with
if ((bool) Mage::helper('example_newsletter')->getNewsletterSubscriptionMailEnabled()) {
    if ($this->getIsStatusChanged() && $status == self::STATUS_UNSUBSCRIBED) {
        $this->sendUnsubscriptionEmail();
    } elseif ($this->getIsStatusChanged() && $status == self::STATUS_SUBSCRIBED) {
        $this->sendConfirmationSuccessEmail();
    }
}
Finally in the unsubscribe method the following code
$this->sendUnsubscriptionEmail();
Should be replaced with
if ((bool) Mage::helper('example_newsletter')->getNewsletterSubscriptionMailEnabled()) {
    $this->sendUnsubscriptionEmail();
}


With this code, you check if the default mails are set to enable/disable mode, and accordingly decide whether to execute the code or not.

Conclusion

When you are already using a third party service for your newsletters, it makes sense to disable the default newsletter that comes with Magento. To disable, you can use the code given in this tutorial. This code checks whether the default newsletter has been enabled/disabled, and accordingly decides whether or not to execute the code.

Note: It is always a good idea to backup the default code before executing a new code.

Author Bio:

Deepa is a passionate blogger associated with Silver Touch Technologies., a leading Magento Development Company UK. If you are looking to hire Magento Developer UK then just get in touch with her.

No comments:

Post a Comment