Quantcast
Viewing all articles
Browse latest Browse all 19

Disabling Magento’s default newsletter transactional emails

Not everyone is satisfied with Magento’s options regarding newsletters so they choose to use some 3rd party service (like MailChimp). In that case it might be a good idea to disable Magento’s default newsletter transactional emails and let that other 3rd party service take care of emails.

In this tutorial I’ll demonstrate how to disable those default Magento’s newsletter transactional emails.

For start we’ll rewrite core model in our config.xml

<newsletter>
	<rewrite>
		<subscriber>Inchoo_Newsletter_Model_Newsletter_Subscriber</subscriber>
	</rewrite>
</newsletter>

It might be a good idea to put config option so store owner can turn on / off default emails anytime he wants to.

We’ll start that by creating system.xml and putting this block of code in it:

<?xml version="1.0"?>
 
<config>
    <tabs>
        <inchoo_tab translate="label">
            <label>Inchoo</label>
            <sort_order>200</sort_order>
        </inchoo_tab>
    </tabs>
 
    <sections>
        <inchoo_newsletter translate="label">
            <label>Newsletter Configuration</label>
            <tab>inchoo_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>
        </inchoo_newsletter>
    </sections>
</config>

Next we’ll add path to our config option inside Helper class and a method to retrieve its value:

class Inchoo_Newsletter_Helper_Data extends Mage_Core_Helper_Abstract
{
    const XML_PATH_NEWSLETTER_MAILS  = 'inchoo_newsletter/newsletter_subscription_transactional_mails/enabled';
 
    public function getNewsletterSubscriptionMailEnabled()
    {
        return Mage::getStoreConfig(self::XML_PATH_NEWSLETTER_MAILS);
    }
}

Final step is editing few core methods from the class which we previously rewrote.
Our new model class is Inchoo_Newsletter_Model_Newsletter_Subscriber and it must extend core class Mage_Newsletter_Model_Subscriber. Name of the class depends on your module, however, we always need to extend core class, otherwise it will not work.

You’ll need to copy 3 methods from the core class into our new class and make small adjustments to them.

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

Inside subscribe method find following

if ($isConfirmNeed === true
	&& $isOwnSubscribes === false
) {
	$this->sendConfirmationRequestEmail();
} else {
	$this->sendConfirmationSuccessEmail();
}

And replace it with our

if ((bool) Mage::helper('inchoo_newsletter')->getNewsletterSubscriptionMailEnabled()) {
    if ($isConfirmNeed === true && $isOwnSubscribes === false) {
        $this->sendConfirmationRequestEmail();
    } else {
        $this->sendConfirmationSuccessEmail();
    }
}

For subscribeCustomer method find:

if ($this->getIsStatusChanged() && $status == self::STATUS_UNSUBSCRIBED) {
    $this->sendUnsubscriptionEmail();
} elseif ($this->getIsStatusChanged() && $status == self::STATUS_SUBSCRIBED) {
    $this->sendConfirmationSuccessEmail();
}

And replace it with:

if ((bool) Mage::helper('inchoo_newsletter')->getNewsletterSubscriptionMailEnabled()) {
    if ($this->getIsStatusChanged() && $status == self::STATUS_UNSUBSCRIBED) {
        $this->sendUnsubscriptionEmail();
    } elseif ($this->getIsStatusChanged() && $status == self::STATUS_SUBSCRIBED) {
        $this->sendConfirmationSuccessEmail();
    }
}

In the last method unsubscribe find:

$this->sendUnsubscriptionEmail();

And replace it with:

if ((bool) Mage::helper('inchoo_newsletter')->getNewsletterSubscriptionMailEnabled()) {
    $this->sendUnsubscriptionEmail();
}

What this means is that we’re first checking whether or not our config option is set to enabled or disabled. If it’s set to disabled then do not execute the code inside if statement which is charge of sending those transactional emails.

The post Disabling Magento’s default newsletter transactional emails appeared first on Inchoo.


Viewing all articles
Browse latest Browse all 19

Trending Articles