Unable to print error message in foreach in magento admin

468 Views Asked by At

Hi i have added a new mas action in the sales order grid which allow create batch invoices. For this my controler file is

<?php
   class Iclp_Batchupdate_IndexController extends Mage_Adminhtml_Controller_Action


  public function batchinvoiceAction ()
   {
        $already = " already ";       
        $refererUrl = $this->getRequest()->getServer('HTTP_REFERER');
        $this->loadLayout();
        $this->renderLayout();
        $orderIds = explode(",",$this->getRequest()->getParam('order_ids'));

        foreach ($orderIds as $orderIdss) {
        $order = Mage::getModel('sales/order')->load($orderIdss);
        //echo $orderIdss ."<br/>";
        //echo "already ".$order->getStatusLabel();
        try
        {
            if(!$order->canInvoice())
                        {
                echo   Mage::getSingleton('core/session')->addError($orderIdss.$already.$order->getStatusLabel());


            }
                $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
                    if (!$invoice->getTotalQty()) {
                    Mage::throwException(Mage::helper('core')->__('Cannot create an invoice without products.'));
                    }
            $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
            $invoice->register();
            $transactionSave = Mage::getModel('core/resource_transaction')->addObject($invoice)->addObject($invoice->getOrder());
            $transactionSave->save();
            $order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true)->save();
            //echo "Invoice are created";
        }
        catch (Mage_Core_Exception $e) {

        }

    }

                //A Success Message
             Mage::getSingleton('core/session')->addSuccess("Some success message");

            //A Error Message
             Mage::getSingleton('core/session')->addError("Some error message");

            //A Info Message (See link below)
             Mage::getSingleton('core/session')->addNotice("This is just a FYI message...");

            //These lines are required to get it to work
            session_write_close();


        $this->getResponse()->setRedirect($refererUrl);

   }



}

every thing is working fine but the problem is it is not printing the error message in foreach in above code

if(!$order->canInvoice())
                        {
                echo Mage::getSingleton('core/session')->addError($orderIdss.$already.$order->getStatusLabel());


            }

but the bottom error message are displayed properly. MOreover if i extend the class with front-action than it also prints the foreach messages. Please suggest where i am doing the mistake

1

There are 1 best solutions below

0
On

You should add your errors and messages to admintml/session and not to core/session when you are in adminhtml. That should display the message correctly. You shouldn't need session_write_close();. There is also no need to echo the message, that should be handled automatically by Magento after the redirect.

There is also no need to call $this->loadLayout(); and $this->renderLayout(); because you are redirecting at the end.

Finally, regarding the redirect, you should not read the referrer yourself, Magento can to that for you more reliably. Just use the $this->_redirectReferer(); method instead of $this->getResponse()->setRedirect($refererUrl);.