Question

In Mage 1.5.1.0 with the ASchroder.com SMTP Pro Email extension, I have things properly configured, as far as I can tell, because when an order is placed and the status is pending* I get the order email. However, when the status is immediately processing*, I do not get the order email.

*Immediately set status, no further action taken, either via Magento or the related payment processor gateway.

Is this behavior by default? If yes, how can I set Mage to send email when an order with ANY status is submitted?

Was it helpful?

Solution

Just updating the status/state of the order does not trigger any emails to be sent. The most relevant code is in

app/code/core/Mage/Sales/Model/Order.php

protected function _setState($state, $status = false, $comment = '', $isCustomerNotified = null, $shouldProtectState = false)
{
    // attempt to set the specified state
    if ($shouldProtectState) {
        if ($this->isStateProtected($state)) {
            Mage::throwException(Mage::helper('sales')->__('The Order State "%s" must not be set manually.', $state));
        }
    }
    $this->setData('state', $state);

    // add status history
    if ($status) {
        if ($status === true) {
            $status = $this->getConfig()->getStateDefaultStatus($state);
        }
        $this->setStatus($status);
        $history = $this->addStatusHistoryComment($comment, false); // no sense to set $status again
        $history->setIsCustomerNotified($isCustomerNotified); // for backwards compatibility
    }
    return $this;
}

which does not include any calls to the email sending code. This is usually handled by each individual payment method and can differ substantially. Some will send the order confirmation email, some will send the invoice confirmation email only. If your payment method does not send any emails at all this seems like an oversight during development. For example in

/app/code/core/Mage/Paypal/Model/Express/Checkout.php you will find $order->sendNewOrderEmail() to trigger the email sending part.

If all payment methods are not sending any emails check your email sending settings under System > Configuration > Sales Email if this is not disabled it is time to further check your email configuration and server/Magento error logs for further clues as to why no emails are being sent once you have confirmed what your code is meant to send.

OTHER TIPS

You can use Magento event/observer.
1. Add observer for sales_order_save_after event:

<global>
    <events>
        <sales_order_save_after>
            <observers>
                <some_module>
                    <class>Some_Module_Model_Observer</class>
                    <method>salesOrderSaveAfter</method>
                </some_module>
            </observers>
        </sales_order_save_after>
    </events>
</global>

2.Send email in the observer if new order email was not sent already:

class Some_Module_Model_Observer
{
    public function salesOrderSaveAfter($observer)
    {
        /**
         * @var $order Mage_Sales_Model_Order
         */
        $order = $observer->getDataObject();
        if(!$order->getEmailSent()){
            try{
                $order->sendNewOrderEmail();
                $historyItem = Mage::getResourceModel('sales/order_status_history_collection')
                    ->getUnnotifiedForInstance($order, Mage_Sales_Model_Order::HISTORY_ENTITY_NAME);
                //track history
                if ($historyItem) {
                    $historyItem->setIsCustomerNotified(1);
                    $historyItem->save();
                }
            }catch (Exception $e){
                //some exception handling
            }
        }
    }
}

By the way sales_order_save_after event fires when order status changed via admin panel or payment module and your observer sends email if order confirmation email not sent.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top