0

I am trying to add a mass action of creating the invoice, in my Magento store, which has been built with Magento - 1.9. The code files are as below.

But when I select the orders and click on the 'Invoice - Capture Online' option. It is getting redirected to the website instead of the admin end. And it is not creating the invoices either. Can anyone kindly let me know why this is happening?

app/etc/modules/MiscWS_MassActions.xml

<?xml version="1.0"?>
<config>
    <modules>
        <MiscWS_MassActions>
            <active>true</active>
            <codePool>local</codePool>
        </MiscWS_MassActions>
    </modules>
</config>

app/code/local/MiscWS/MassActions/etc/config.xml

<config>
    <modules>
        <MiscWS_MassActions>
            <version>0.1.0</version>
        </MiscWS_MassActions>
    </modules>
    <admin>
          <routers>
             <MiscWS_MassActions>
                <use>admin</use>
                <args>
                   <module>MiscWS_MassActions</module>
                   <frontName>miscwsmassactions</frontName>
                </args>
             </MiscWS_MassActions>
          </routers>
    </admin>
    <adminhtml>
        <events>
            <core_block_abstract_prepare_layout_before>
                <observers>
                    <MiscWS_MassActions_Model_Observer>
                        <type>singleton</type>
                        <class>MiscWS_MassActions_Model_Observer</class>
                        <method>addMassAction</method>
                    </MiscWS_MassActions_Model_Observer>
                </observers>
            </core_block_abstract_prepare_layout_before>
        </events>
    </adminhtml>
</config>

app/code/local/MiscWS/MassActions/Model/Observer.php

<?php
class MiscWS_MassActions_Model_Observer
{
    public function addMassAction($observer)
    {
        $block = $observer->getEvent()->getBlock();
        if(get_class($block) =='Mage_Adminhtml_Block_Widget_Grid_Massaction'
            && $block->getRequest()->getControllerName() == 'sales_order')
        {
            $block->addItem('miscwsmassactions', array(
                'label' => 'Invoice - Capture Online',
                'url' => Mage::app()->getStore()->getUrl('miscwsmassactions/adminhtml_massActions/massInvoiceCaptureOnline')
            ));
        }
    }
}

app/code/local/MiscWS/MassActions/Controllers/Adminhtml/MassActionsController.php

<?php

class MiscWS_MassActions_Adminhtml_MassActionsController extends Mage_Adminhtml_Controller_Action
{
  public function massInvoiceCaptureOnlineAction()
    {
        $orderIds = $this->getRequest()->getPost('order_ids', array());
        $countInvoicedOrder = 0;
        $countNonInvoicedOrder = 0;


        foreach ($orderIds as $orderId) {
            $order = Mage::getModel('sales/order')->load($orderId);
            if ($order->canInvoice()) {
                $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
                if( $invoice->getTotalQty() ) {
                    $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
                    $invoice->register();
                    $transactionSave = Mage::getModel('core/resource_transaction')
                        ->addObject($invoice)
                        ->addObject($invoice->getOrder());
                    $transactionSave->save();                    
                    $countInvoicedOrder++;
                } else {
                    $countNonInvoicedOrder++;
                }

            } else {
                $countNonInvoicedOrder++;
            }
        }
        if ($countNonInvoicedOrder) {
            if ($countInvoicedOrder) {
                $this->_getSession()->addError($this->__('%s order(s) were not invoiced.', $countNonInvoicedOrder));
            } else {
                $this->_getSession()->addError($this->__('No order(s) were invoiced.'));
            }
        }
        if ($countInvoicedOrder) {
            $this->_getSession()->addSuccess($this->__('%s order(s) have been invoiced.', $countInvoicedOrder));
        }
        $this->_redirect('adminhtml/sales_order/');
    }
}

Reference: http://misc.ws/2014/12/27/adding-mass-actions-to-magento-orders/

1

The issue is with your controller file path. According to my knowledge you have put the MassActionsController.php under

/app/code/local/MiscWS/MassActions/controllers/

You need to put the file under below location:

/app/code/local/MiscWS/MassActions/controllers/Adminhtml/

Also for best practice dont use the below line in your observer:

'url' => Mage::app()->getStore()->getUrl('miscwsmassactions/adminhtml_massActions/massInvoiceCaptureOnline')

Replace it with the below code:

'url' => Mage::helper('adminhtml')->getUrl('miscwsmassactions/adminhtml_massActions/massInvoiceCaptureOnline', array('_secure' => true))
  • The controller exists in the same location that you have mentioned above. And I also changed the URL path, with the code that you had provided above. Still, on selecting the records and clicking submit, it is still redirecting me to the main website instead of staying in the backend and updating the selected records. – Akshay Vasu Jul 6 '18 at 9:35
  • for your reference, I have added the complete path of the files too. – Akshay Vasu Jul 6 '18 at 9:35
  • I have tried the same and its working perfectly for me. Do you want that created extension? – Sukumar Gorai Jul 6 '18 at 9:36
  • dropbox.com/s/mylgc0lpu5dycrf/MiscMS_MassActions.zip?dl=0 This is working code and tested as well. – Sukumar Gorai Jul 6 '18 at 9:40
  • Remove your extension and install it. – Sukumar Gorai Jul 6 '18 at 9:40

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.