2

I've created a function for mass order status change.

Here is my code:

public function NewMass1Action()
{
    $orderIds = $this->getRequest()->getPost('order_ids', array());
    $countHoldOrder = 0;

    foreach ($orderIds as $orderId) {
        $order = Mage::getModel('sales/order')->load($orderId);
        if ($order->canHold()) {
            $order->masscustom1()
                ->save();
            $countHoldOrder++;
        }
    }

    if ($countHoldOrder) {
        $this->_getSession()->addSuccess($this->__('%s order(s) have changed status.', $countHoldOrder));
    }

    $this->_redirectUrl(Mage::helper('adminhtml')->getUrl('adminhtml/sales_order/index'));
}

The problem is that this function is not sending an email to the customer when his order status is changed.

How can i make this function send an email for the status change?

2 Answers 2

4

Source: http://www.atwix.com/magento/emails-for-custom-events/


Create a file /app/etc/modules/Namespace_Custmail.xml with following content:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Namespace_Custmail>
            <active>true</active>
            <codePool>community</codePool>
        </Namespace_Custmail>
    </modules>
</config>

/app/code/community/Namespace/Custmail/etc/config.xml

<config>
    <modules>
        <Namespace_Custmail>
            <version>1.0.0</version>
        </Namespace_Custmail>
    </modules>

    <global>
        <models>
            <custmail>
                <class>Namespace_Custmail_Model</class>
            </custmail>
        </models>
        <events>
            <sales_order_save_commit_after>
                <observers>
                    <mail_status_change>
                        <type>singleton</type>
                        <class>custmail/observer</class>
                        <method>invoicedStatusChange</method>
                    </mail_status_change>
                </observers>
            </sales_order_save_commit_after>
        </events>
        <template>
            <email>
                <custom_order_tpl module="Namespace_Custmail">
                    <label>Status Mail Invoice</label>
                    <file>statusmail_processing.html</file>
                    <type>html</type>
                </custom_order_tpl>
            </email>
        </template>
    </global>
</config>

/app/code/community/Namespace/Custmail/Model/Observer.php

class Namespace_Custmail_Model_Observer
{
    public function invoicedStatusChange($event)
    {
        $order = $event->getOrder();
        $orderStatus = $order->getStatus();
        if ($order->getState() == Mage_Sales_Model_Order::STATE_CANCELED)
            $this->_sendStatusMail($order);
    }

    private  function _sendStatusMail($order)
    {
        $emailTemplate  = Mage::getModel('core/email_template');

        $emailTemplate->loadDefault('custom_order_tpl');
        $emailTemplate->setTemplateSubject('Your order was holded');

        // Get General email address (Admin->Configuration->General->Store Email Addresses)
        $salesData['email'] = Mage::getStoreConfig('trans_email/ident_general/email');
        $salesData['name'] = Mage::getStoreConfig('trans_email/ident_general/name');

        $emailTemplate->setSenderName($salesData['name']);
        $emailTemplate->setSenderEmail($salesData['email']);

        $emailTemplateVariables['username']  = $order->getCustomerFirstname() . ' ' . $order->getCustomerLastname();
        $emailTemplateVariables['order_id'] = $order->getIncrementId();
        $emailTemplateVariables['store_name'] = $order->getStoreName();
        $emailTemplateVariables['store_url'] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
        $emailTemplate->send($order->getCustomerEmail(), $order->getStoreName(), $emailTemplateVariables);
    }
}

/app/locale/en_US/template/email/statusmail_processing.html

<div>
    <h1>Dear {{var username}}</h1>
    <p>Your order #{{var order_id}} was canceled for some reasons… </p>
    <div>{{var storename}} ({{var store_url}})</div>
</div>
0

bellow code i have tested try it.

public function automaticallyInvoiceShipCompleteOrder($observer)
    {
        $order = $observer->getEvent()->getOrder();

        $orders = Mage::getModel('sales/order_invoice')->getCollection()
                        ->addAttributeToFilter('order_id', array('eq'=>$order->getId()));
        $orders->getSelect()->limit(1);  

        if ((int)$orders->count() !== 0) {
            return $this;
        }

        if ($order->getState() == Mage_Sales_Model_Order::STATE_NEW) {

            try {
                if(!$order->canInvoice()) {
                    $order->addStatusHistoryComment('Custom_Invoicer: Order cannot be invoiced.', false);
                    $order->save();  
                }

                //START Handle Invoice
                $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();

                $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
                $invoice->register();

                $invoice->getOrder()->setCustomerNoteNotify(true);          
                $invoice->getOrder()->setIsInProcess(true);
                $invoice->sendEmail(true, '');
                $order->addStatusHistoryComment('Automatically INVOICED by Custom_Invoicer.', false);

                $transactionSave = Mage::getModel('core/resource_transaction')
                    ->addObject($invoice)
                    ->addObject($invoice->getOrder());

                $transactionSave->save();

                //END Handle Invoice

                //START Handle Shipment
                //~ $shipment = $order->prepareShipment();
                //~ $shipment->register();
 //~ 
                //~ $order->setIsInProcess(true);
                //~ $order->addStatusHistoryComment('Automatically SHIPPED by Perception_Invoicer.', false);
 //~ 
                //~ $transactionSave = Mage::getModel('core/resource_transaction')
                    //~ ->addObject($shipment)
                    //~ ->addObject($shipment->getOrder())
                    //~ ->save();
                //END Handle Shipment
            } catch (Exception $e) {
                $order->addStatusHistoryComment('Custom_Invoicer: Exception occurred during automaticallyInvoiceShipCompleteOrder action. Exception message: '.$e->getMessage(), false);
                $order->save();
            }                
        }

    return $this;        
    }

I hope this will help you.

Your Answer

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

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