1

I want to create new mass action inside Order grid which will create new Shipment but I have problem with my action. When I click submit button my action don't work but only redirect me on homepage.

My app/code/local/CFO/ShipAction/etc/config.xml

<config>
<modules>
    <CFO_ShipAction>
        <version>0.1.0</version>
    </CFO_ShipAction>
</modules>
<admin>
    <routers>
        <cfoshipaction>
            <use>admin</use>
            <args>
                <module>CFO_ShipAction_Adminhtml</module>
                <frontName>cfoshipaction</frontName>
            </args>
        </cfoshipaction>
    </routers>
</admin>
<adminhtml>
    <events>
        <core_block_abstract_prepare_layout_before>
            <observers>
                <CFO_ShipAction_Model_Observer>
                    <type>singleton</type>
                    <class>CFO_ShipAction_Model_Observer</class>
                    <method>addShipAction</method>
                </CFO_ShipAction_Model_Observer>
            </observers>
        </core_block_abstract_prepare_layout_before>
    </events>
</adminhtml>
</config>

My app/code/local/CFO/ShipAction/Model/Observer.php

class CFO_ShipAction_Model_Observer
{
    public function addShipAction($observer){
        $block = $observer->getEvent()->getBlock();
        if(get_class($block) =='Mage_Adminhtml_Block_Widget_Grid_Massaction'
            && $block->getRequest()->getControllerName() == 'sales_order')
        {
            $block->addItem('cfoshipaction', array(
                'label' => 'Create shipment',
                'url' => Mage::app()->getStore()->getUrl('cfoshipaction/adminhtml_massactions/createShipment')
            ));
        }               
    }
}

And app/code/local/CFO/ShipAction/controllers/Adminhtml/MassActionsController.php

require_once 'Mage/Adminhtml/controllers/Action.php';

class CFO_ShipAction_Adminhtml_MassActionsController extends Mage_Adminhtml_Controller_Action {        
    public function createShipmentAction() {
        $orderIds = $this->getRequest()->getPost('order_ids', array());     
            foreach ($orderIds as $orderId){
                try {
                    $order = Mage::getModel('sales/order')->load($orderId);
                    if($order->canShip()) {
                        //Create shipment
                        $shipmentid = Mage::getModel('sales/order_shipment_api')
                                        ->create($order->getIncrementId(), array());
                        //Add tracking information
                        $ship = Mage::getModel('sales/order_shipment_api')
                                        ->addTrack($order->getIncrementId(), array());       
                    }
                }catch (Mage_Core_Exception $e) {
                    print_r($e);
                }
            }
            $this->_redirect('adminhtml/sales_order/');
    }
}  

I'm know that problem is with my router, but I don't know where. I tried 2 options for initializing my router but both not help. Can anybody see what's missing? Thanks!

EDIT:

There is my other config.xml with other router which I've tried to use but sill noting:

<config>
...
<admin>
    <routers>
        <adminhtml>
            <args>
                <CFO_ShipAction before="Mage_Adminhtml">CFO_ShipAction_Adminhtml</CFO_ShipAction>
            </args>
        </adminhtml>
    </routers>
</admin>
...
</config>

Thanks for help !

  • So, are you saying your observer works, but something doesn't work in the controller action? – musicliftsme May 25 '16 at 17:51
  • Yes, I have redirected to homepage when I try launch mywebsite.com/index.php/cfoshipaction/adminhtml_massactions/createShipment/ – Bananowy May 27 '16 at 11:11
1

Problem resolved !

There was a mistake in my controller. I've chosen other type of router initialize (like my edits) but I forgot about modules container, so now my config.xml look like:

...
<admin>
    <routers>
        <adminhtml>
            <args>
                <modules>
                    <shipactions after="Mage_Adminhtml">CFO_ShipAction_Adminhtml</shipactions>
                </modules>
            </args>
        </adminhtml>
    </routers>
</admin>
...

Moreover there was a problem with name of Controller. When my name of file is MassActionsController with createShipment Action then my url is adminhtml/massActions/createShipment.

Last thing is disable the Secret Key in URLs. You can find it in System>Configuration>Security>Add Secret Key to URLs.

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.