4

Is there a way to disable a Magento Order Confirmation Email for a particular customer group ?

I won't mind someone giving me a guide on how to create a custom module for this function.

Thanks

3
  • Have you found a solution?
    – sv3n
    Jul 7, 2017 at 21:54
  • Nope Not yet. Still working on the project Jul 14, 2017 at 11:21
  • 1) See updated answer. 2) Can you give please post some details about your M2E settings? (multiple account, assigned customer groups etc.) It's an interessting question, that I can use too ... but it's not on top of my priority list ... so please add some info to make it reproducable.
    – sv3n
    Jul 14, 2017 at 17:22

2 Answers 2

0

For this, you have to rewrite the queueNewOrderEmail() function from: app/code/core/Mage/Sales/Model/Order.php to your local codePool then put this:

public function queueNewOrderEmail($forceMode = false)
    {
        $storeId = $this->getStore()->getId();
        if (!Mage::helper('sales')->canSendNewOrderEmail($storeId)) {
            return $this;
        }

        // Get the destination email addresses to send copies to
        $copyTo = $this->_getEmails(self::XML_PATH_EMAIL_COPY_TO);
        $copyMethod = Mage::getStoreConfig(self::XML_PATH_EMAIL_COPY_METHOD, $storeId);

        // Start store emulation process
        /** @var $appEmulation Mage_Core_Model_App_Emulation */
        $appEmulation = Mage::getSingleton('core/app_emulation');
        $initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId);

        try {
            // Retrieve specified view block from appropriate design package (depends on emulated store)
            $paymentBlock = Mage::helper('payment')->getInfoBlock($this->getPayment())
                ->setIsSecureMode(true);
            $paymentBlock->getMethod()->setStore($storeId);
            $paymentBlockHtml = $paymentBlock->toHtml();
        } catch (Exception $exception) {
            // Stop store emulation process
            $appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
            throw $exception;
        }

        // Stop store emulation process
        $appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);

        // Retrieve corresponding email template id and customer name
        if ($this->getCustomerIsGuest()) {
            $templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_GUEST_TEMPLATE, $storeId);
            $customerName = $this->getBillingAddress()->getName();
        } else {
            $templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE, $storeId);
            $customerName = $this->getCustomerName();
        }

        /** @var $mailer Mage_Core_Model_Email_Template_Mailer */
        $mailer = Mage::getModel('core/email_template_mailer');
        /** @var $emailInfo Mage_Core_Model_Email_Info */
        $emailInfo = Mage::getModel('core/email_info');
        $emailInfo->addTo($this->getCustomerEmail(), $customerName);
        if ($copyTo && $copyMethod == 'bcc') {
            // Add bcc to customer email
            foreach ($copyTo as $email) {
                $emailInfo->addBcc($email);
            }
        }
        $mailer->addEmailInfo($emailInfo);

        // Email copies are sent as separated emails if their copy method is 'copy'
        if ($copyTo && $copyMethod == 'copy') {
            foreach ($copyTo as $email) {
                $emailInfo = Mage::getModel('core/email_info');
                $emailInfo->addTo($email);
                $mailer->addEmailInfo($emailInfo);
            }
        }

        // Set all required params and send emails
        $mailer->setSender(Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $storeId));
        $mailer->setStoreId($storeId);
        $mailer->setTemplateId($templateId);
        $mailer->setTemplateParams(array(
            'order'        => $this,
            'billing'      => $this->getBillingAddress(),
            'payment_html' => $paymentBlockHtml
        ));

        /** @var $emailQueue Mage_Core_Model_Email_Queue */
        $emailQueue = Mage::getModel('core/email_queue');
        $emailQueue->setEntityId($this->getId())
            ->setEntityType(self::ENTITY)
            ->setEventType(self::EMAIL_EVENT_NAME_NEW_ORDER)
            ->setIsForceCheck(!$forceMode);
        //The add
        $customerGroups = array(1, 3); //the customer group id's that you don't send Email
        $currentCustomerGroup = Mage::getSingleton('customer/session')->getCustomerGroupId();
        if(!in_array($currentCustomerGroup, $customerGroups)) {
            $mailer->setQueue($emailQueue)->send();
            $this->setEmailSent(true);
        }

        $this->_getResource()->saveAttribute($this, 'email_sent');

        return $this;
    }
2
  • Hi, Thanks for your input. This didn’t work for me. Jun 30, 2017 at 12:49
  • It should work, i tested it ! maybe you haven't rewrite correctly the model ! reference you to this: http://inchoo.net/magento/overriding-magento-blocks-models-helpers-and-controllers/
    – PЯINCƎ
    Jul 1, 2017 at 0:11
0

You can create a small extension like this:

app/code/local/My/Module/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <My_Module>
            <version>0.0.1</version>
        </My_Module>
    </modules>
    <global>
        <models>
            <my_module>
                <class>My_Module_Model</class>
            </my_module>
        </models>
        <events>
            <!-- onepage checkout -->
            <checkout_type_onepage_save_order_after>
                <observers>
                    <disable_new_order_email>
                        <class>my_module/observer</class>
                        <method>disableNewOrderEmail</method>
                    </disable_new_order_email>
                </observers>
            </checkout_type_onepage_save_order_after>
            <!-- multishipping checkout -->
            <checkout_type_multishipping_create_orders_single>
                <observers>
                    <disable_new_order_email>
                        <class>my_module/observer</class>
                        <method>disableNewOrderEmail</method>
                    </disable_new_order_email>
                </observers>
            </checkout_type_multishipping_create_orders_single>
        </events>
    </global>
</config>

app/code/local/My/Module/Model/Observer.php

class My_Module_Model_Observer extends Mage_Core_Model_Observer
{
    public function disableNewOrderEmail(Varien_Event_Observer $observer)
    {
        $order = $observer->getOrder();
        $noEmail = array(0, 1, 2, 3); // your customer group IDs
        if (in_array($order->getCustomerGroupId(), $noEmail)) {
            $order->setCanSendNewEmailFlag(false);
        }
    }
}

Reference to the related code:

Mage_Checkout_Model_Type_Onepage::saveOrder()

public function saveOrder()
{
    ...
        Mage::dispatchEvent('checkout_type_onepage_save_order_after',
            array('order'=>$order, 'quote'=>$this->getQuote()));
        ...
        if (!$redirectUrl && $order->getCanSendNewEmailFlag()) {
            try {
                $order->queueNewOrderEmail();
            } catch (Exception $e) {
                Mage::logException($e);
            }
        }
    ...
}

Edit:

For M2E you can try to modify/rewrite

  • Ess_M2ePro_Model_Amazon_Order
  • Ess_M2ePro_Model_Ebay_Order

Try this:

public function afterCreateMagentoOrder()
{
    if ($this->getAmazonAccount()->isMagentoOrdersCustomerNewNotifyWhenOrderCreated()) {
        $order = $this->getParentObject()->getMagentoOrder();
        $noEmail = array(0, 1, 2, 3); // your customer group IDs
        if (in_array($order->getCustomerGroupId(), $noEmail)) {
            if (method_exists($order, 'queueNewOrderEmail')) {
                $order->queueNewOrderEmail(false);
            } else {
                $order->sendNewOrderEmail();
            }
        }
    }

    if ($this->isFulfilledByAmazon() && !$this->getAmazonAccount()->isMagentoOrdersFbaStockEnabled()) {
        Mage::dispatchEvent('m2epro_amazon_fba_magento_order_place_after', array(
            'magento_order' => $this->getParentObject()->getMagentoOrder()
        ));
    }
}

Edit 2:

In M2Es account configuration you have to set a customer group that is used for eBay/Amazon order creation. So, for one account the customer group is always the same!

If you have more the one account you can set differnt groups (like amazon-uk, amazon-it) and use the code from above. (I haven't tested it yet, but i can do it next week)

2
  • Thanks for your input. This wouldn’t work for me because the orders in questions are Amazon orders that are been generated my M2E, not the normal Magento orders. Thanks Jun 30, 2017 at 12:51
  • Hi, Thanks for getting back in touch. Here is my setup. I have two customer groups named Amazon Customers and EBay Customer with this corresponding Group Ids 7 & 8. So all orders that’s comes through any of the Amazon Store fronts (like amazon-uk, amazon-it) goes into the Amazon Customers with Group id 7 likewise the Ebay Orders. So I am trying to stop Magento from sending Order Confirmation emails or any email at all to all the customers that belongs to the Amazon Customers with Group id 7. Thanks Jul 17, 2017 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.