0

I added two button in order view it's working fine but i want to send email to that change state

My Abstract file

 <?php

class Ar_Orderstatus_Model_Observer
{

    public function addOrderstatusAction($observer)
    {
        $block = $observer->getEvent()->getBlock();
        if ($block instanceof Mage_Adminhtml_Block_Sales_Order_View) {
            $order = $this->_initOrder();
            if (!$order->isCanceled() && $order->canShip()) {
                $message = Mage::helper('sales')->__('Are you sure you want to Change Status?');

                $block->addButton('rto', 
                    array( 'label' => Mage::helper('sales')->__('RTO'), 
                        'onclick' => "confirmSetLocation('{$message}', '{$block->getUrl('orderstatus/adminhtml_index/rto')}')", 'class' => 'go' ));

               $block->addButton('in_shipment', 
                array( 'label' => Mage::helper('sales')->__('In Shipement'), 
                'onclick' => "confirmSetLocation('{$message}', '{$block->getUrl('orderstatus/adminhtml_index/in_shipment')}')", 'class' => 'go' ));  
            }
        }
    }

    protected function _initOrder()
    {
        $id = Mage::app()->getRequest()->getParam('order_id');
        $order = Mage::getModel('sales/order')->load($id);
        return $order;
    }
}

Controller file

<?php
class Ar_Orderstatus_Adminhtml_IndexController extends Mage_Adminhtml_Controller_Action 
{
    protected function _initOrder()
    {
        $id = $this->getRequest()->getParam('order_id');
        $order = Mage::getModel('sales/order')->load($id);

        if (!$order->getId()) {
            $this->_getSession()->addError($this->__('This order no longer exists.'));
            $this->_redirect('*/*/');
            $this->setFlag('', self::FLAG_NO_DISPATCH, true);
            return false;
        }
        Mage::register('sales_order', $order);
        Mage::register('current_order', $order);
        return $order;
    }


    public function rtoAction()
    {
        if ($order = $this->_initOrder()) {
            try {
                $order->setState('rto', true)
                    ->save();
                $order->sendOrderUpdateEmail(true, 'test');
                $this->_getSession()->addSuccess(
                    $this->__('The order state has been changed.')
                );
            }
            catch (Mage_Core_Exception $e) {
                $this->_getSession()->addError($e->getMessage());
            }
            catch (Exception $e) {
                $this->_getSession()->addError($this->__('The order state has not been changed.'));
                Mage::logException($e);
            }
            $this->_redirect('adminhtml/sales_order/view', array('order_id' => $order->getId()));
        }

    }

    public function shipmentAction()
    {
        if ($order = $this->_initOrder()) {
            try {
                $order->setState('shipment', true)
                    ->save();
                $order->sendOrderUpdateEmail(true, 'test');
                $this->_getSession()->addSuccess(
                    $this->__('The order state has been changed.')
                );
            }
            catch (Mage_Core_Exception $e) {
                $this->_getSession()->addError($e->getMessage());
            }
            catch (Exception $e) {
                $this->_getSession()->addError($this->__('The order state has not been changed.'));
                Mage::logException($e);
            }
            $this->_redirect('adminhtml/sales_order/view', array('order_id' => $order->getId()));
        }

    }
}
  • Pls share your Controller code – Abdul Dec 10 '15 at 5:59
0

Add bellow code in your controller file

<?php
class Ar_Orderstatus_Adminhtml_IndexController extends Mage_Adminhtml_Controller_Action 
{
    protected function _initOrder()
    {
        $id = $this->getRequest()->getParam('order_id');
        $order = Mage::getModel('sales/order')->load($id);

        if (!$order->getId()) {
            $this->_getSession()->addError($this->__('This order no longer exists.'));
            $this->_redirect('*/*/');
            $this->setFlag('', self::FLAG_NO_DISPATCH, true);
            return false;
        }
        Mage::register('sales_order', $order);
        Mage::register('current_order', $order);
        return $order;
    }


    public function rtoAction()
    {
        if ($order = $this->_initOrder()) {
            try {
                $order->setState('rto', true)
                    ->save();
                $order->sendOrderUpdateEmail(true, '');
                $this->_getSession()->addSuccess(
                    $this->__('The order state has been changed.')
                );
            }
            catch (Mage_Core_Exception $e) {
                $this->_getSession()->addError($e->getMessage());
            }
            catch (Exception $e) {
                $this->_getSession()->addError($this->__('The order state has not been changed.'));
                Mage::logException($e);
            }
            $this->_redirect('adminhtml/sales_order/view', array('order_id' => $order->getId()));
        }

    }

    public function shipmentAction()
    {
        if ($order = $this->_initOrder()) {
            try {
                $order->setState('shipment', true)
                    ->save();
                $order->sendOrderUpdateEmail(true, '');
                $this->_getSession()->addSuccess(
                    $this->__('The order state has been changed.')
                );
            }
            catch (Mage_Core_Exception $e) {
                $this->_getSession()->addError($e->getMessage());
            }
            catch (Exception $e) {
                $this->_getSession()->addError($this->__('The order state has not been changed.'));
                Mage::logException($e);
            }
            $this->_redirect('adminhtml/sales_order/view', array('order_id' => $order->getId()));
        }

    }
}
  • No i didn't get any mail – Magento 2 Dec 10 '15 at 6:03
  • check my update question – Magento 2 Dec 10 '15 at 6:04
  • I tried my end it is working fine. Pls check your eamil or send email with valid email id – Abdul Dec 10 '15 at 6:09
  • After change this controller file. If i click button it redirect to 404 – Magento 2 Dec 10 '15 at 6:11
  • In your Observer replace $block->getUrl('orderstatus/adminhtml_index/in_shipment') to $block->getUrl('orderstatus/adminhtml_index/shipment') – Abdul Dec 10 '15 at 6:14
0

try this code for sending the email..

public function rtoAction()
{
    if ($order = $this->_initOrder()) {
        try {
            $order->setState('rto', true);
            $order->setStatus('rto');
            $history = $order->addStatusHistoryComment('', false);
            $history->setIsCustomerNotified(true);
            $order->save();
            $order->sendOrderUpdateEmail(true, 'test');
            $this->_getSession()->addSuccess(
                $this->__('The order state has been changed.')
            );
        }
        catch (Mage_Core_Exception $e) {
            $this->_getSession()->addError($e->getMessage());
        }
        catch (Exception $e) {
            $this->_getSession()->addError($this->__('The order state has not been changed.'));
            Mage::logException($e);
        }
        $this->_redirect('adminhtml/sales_order/view', array('order_id' => $order->getId()));
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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