سؤال

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/

هل كانت مفيدة؟

المحلول

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))
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top