How can I get the selected order ids in custom action in sales order onclick?

How can I get the selected order ids in custom action in sales order onclick?

September 24, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How can I get the selected order ids in custom action in sales order onclick?

Creating Invoices for Selected Orders Using Custom Mass Actions in Magento

In this post, we’ll guide you through creating invoices for selected orders using custom mass actions in your Magento store. We’ll build upon the existing code you’ve shared and provide a complete solution.

Understanding Your Current Setup

You have already set up a new mass action named “Invoice” in your sales order grid. The mass action appears in the grid, but when you select it and click submit, no invoices are generated for the selected orders.

The key issue lies in passing the selected order IDs from the grid to the action URL where the invoice generation logic resides.

Modifying the Mass Action Block

Let’s modify your app/code/local/MagePsycho/Newmodule/Block/Sales/Order/Grid.php file:

<?php
class MagePsycho_Newmodule_Block_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
{
    protected function _prepareMassaction()
    {
        parent::_prepareMassaction();

        // Append new mass action option
        $this->getMassactionBlock()->addItem(
            'newmodule',
            array('label' => $this->__('Invoice'), 
                  'url'   => $this->getUrl('adminhtml/newmodule_action/index'),
                  'additional_data' => array(
                      'orders' => $this->_prepareSelectedOrders(),
                  )
            )
        );
    }

    private function _prepareSelectedOrders()
    {
        $selected = $this->getRequest()->getParam($this->getMassactionBlock()->getFormFieldName());
        if (is_array($selected)) {
            return implode(',', $selected);
        }
        return false;
    }
}

We’ve added the additional_data parameter to the mass action item. This will contain a JSON array of selected order IDs.

Updating the Observer

Since we’re moving the mass action logic to the core block observer, let’s update your app/code/local/MagePsycho/Newmodule/Model/Observer.php:

<?php
class MagePsycho_Newmodule_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')
        {
            $data = $block->getRequest()->getParam('additional_data');
            if (isset($data['orders'])) {
                // Process the selected orders here
                $orderIds = explode(',', $data['orders']);
                foreach ($orderIds as $orderId) {
                    $this->_processOrder($orderId);
                }
            }

            $block->addItem('newmodule', array(
                'label' => 'Invoice',
                'url' => Mage::app()->getStore()->getUrl('adminhtml/newmodule_action/index'),
            ));
        }
    }

    private function _processOrder($orderId)
    {
        // Add your invoice generation logic here
        // Example: Create a new invoice for the order with ID $orderId
        // You can use Magento's API to create an invoice:
        // Mage::getModel('sales/service_order')->prepareInvoice($order);
        // Then save and send the invoice.
    }
}

In this updated observer, we’re checking if there are selected orders in the additional_data parameter. If so, we explode the comma-separated string of order IDs and process each one using your _processOrder method.

Creating the Invoice Generation Controller

You’ll need to create a new controller to handle the invoice generation logic. Create a file named Action.php in app/code/local/MagePsycho/Newmodule/controllers/Adminhtml:

<?php
class MagePsycho_Newmodule_Adminhtml_ActionController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {
        $orderIds = $this->getRequest()->getParam('additional_data');
        if (isset($orderIds['orders'])) {
            // Process the selected orders here
            $orderIds = explode(',', $orderIds['orders']);
            foreach ($orderIds as $orderId) {
                $this->_processOrder($orderId);
            }
        }

        $this->_redirect('adminhtml/sales_order');
    }

    private function _processOrder($orderId)
    {
        // Add your invoice generation logic here
        // Example: Create a new invoice for the order with ID $orderId
        // You can use Magento's API to create an invoice:
        // Mage::getModel('sales/service_order')->prepareInvoice($order);
        // Then save and send the invoice.
    }
}

This controller handles the invoice generation logic when the mass action is submitted. It retrieves the selected order IDs from the additional_data parameter, processes each one using your _processOrder method, and then redirects back to the sales order grid.

Conclusion

By following this guide, you should now be able to create invoices for selected orders in your Magento store using a custom mass action. The key steps involve modifying the mass action block to pass selected order IDs as additional data, updating the observer to process these IDs, and creating a controller to handle the invoice generation logic.

Remember to clear your cache after making these changes to ensure they take effect.

For more advanced customizations in Magento, consider exploring our blog at Laravel Company. We regularly publish detailed tutorials and best practices for e-commerce development.

Extensions Admin Orders Errors

Our network