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
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityIs 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
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;
}
http://inchoo.net/magento/overriding-magento-blocks-models-helpers-and-controllers/
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)