Disable Magento Emailing when updating orders programmatically

By default, in Magento, when an order is updated, an email is sent to the customer notifying them of the change. This even happens when the order is updated programmatically in PHP eg. if you create a shipment, such as in the code below :

$tracking_courier="fedex";
$tracking_id="123456789";
$order = Mage::getModel('sales/order')->loadByIncrementId($SOME_ORDER_ID);
if($order->canShip()) {
 $shipment = Mage::getModel('sales/service_order', $order)->prepareShipment($itemQty);
 $shipment = new Mage_Sales_Model_Order_Shipment_Api();
 $shipmentId = $shipment->create($mag_order_id);
 Mage::log("added shipment");
 $shipment->addTrack($shipmentId, $tracking_courier, 'Tracking ID', $tracking_id);
 Mage::log("added tracking");
}

You can disable this functionality by setting the config using the appropriate XML_PATH_EMAIL constants in the appropriate class.

For example to disable emails from the Shipment class, update the config using the constants for all store views.

// disable emailing out - this temporarily disables sending of emails from the Magento Shipment class for a site with multi-store views
$stores = Mage::app()->getStores();
foreach ($stores as $store) {
 $storeid=$store->getId();
 Mage::app()->getStore($storeid)->setConfig(Mage_Sales_Model_Order_Shipment::XML_PATH_EMAIL_ENABLED, "0");
 Mage::app()->getStore($storeid)->setConfig(Mage_Sales_Model_Order_Shipment::XML_PATH_UPDATE_EMAIL_ENABLED, "0");
 Mage::app()->getStore($storeid)->setConfig(Mage_Sales_Model_Order_Shipment::XML_PATH_EMAIL_COPY_TO, "");
 Mage::app()->getStore($storeid)->setConfig(Mage_Sales_Model_Order_Shipment::XML_PATH_UPDATE_EMAIL_COPY_TO, "");
}

You can also do this for the class Mage_Sales_Model_Order to stop those emails.

Mage::app()->getStore()->setConfig(Mage_Sales_Model_Order::XML_PATH_EMAIL_ENABLED, "0");

Reference is the comments here :
http://inchoo.net/magento/how-to-disable-email-sending-when-programatically-creating-an-order-in-magento/

Leave a Reply