Magento Cronjobs – Send New Order Email Only with Cronjobs

cronemailorders

How can I send The new order Emails using:

$order->sendNewOrderEmail();

only from a cronjob used in My custom Module.

Thanks for help.

Best Answer

I would disable System > Configuration > Sales Email > Order > Enabled

this makes sure that during normal execution it is not send

public function sendNewOrderEmail()
{
    $storeId = $this->getStore()->getId();

    if (!Mage::helper('sales')->canSendNewOrderEmail($storeId)) {
        return $this;
    }

Then in your custom module include something like

    Mage::getConfig()->setNode(
        'default/'.Mage_Sales_Model_Order::XML_PATH_EMAIL_ENABLED, true
    );
    foreach(Mage::app()->getStores() as $storeCode=>$store){
        Mage::getConfig()->setNode(
            "stores/{$storeCode}/".Mage_Sales_Model_Order::XML_PATH_EMAIL_ENABLED, true
        );
    }
    $collection = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('email_sent', 0);
    foreach ($collection as $order){
        $order->sendNewOrderEmail();
    }

The main idea being to override the disabled configuration value at runtime. The code is not tested but should give you a starting point. Further recommended reading from Alan's blog: http://alanstorm.com/magento_loading_config_variables http://alanstorm.com/magento_config_a_critique_and_caching

One issue that you might encounter is a cached value for the above.

Second option would be to duplicate the code from sendNewOrderEmail().

Related Topic