user1625155
user1625155

Reputation: 521

Magento Admin Custom Button on View Order

I am trying to create a button on the Magento View Order page that when I click it, it will email a certain vendor using information that is contained on the order and items in the order.

I have successfully created the module, and the button, I can click the button and it displays a warning type message. But I cannot figure out how to make the button perform the action. Currently the button is just going to a URL.

Here is what I have:

MG_Dropship.xml

    <?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <MG_Dropship>

            <!-- Whether our module is active: true or false -->
            <active>true</active>

            <!-- Which code pool to use: core, community or local -->
            <codePool>local</codePool>

        </MG_Dropship>
    </modules>
</config>

config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <MG_Dropship>
            <version>0.0.1</version>
        </MG_Dropship>
    </modules>

    <!-- Configure our module's behavior in the global scope -->
    <global>

    <blocks>
         <adminhtml>
            <rewrite>
                <sales_order_view>MG_Dropship_Block_Adminhtml_Sales_Order_View</sales_order_view>
            </rewrite>
        </adminhtml>
    </blocks>

    </global>

</config>

View.php

<?php

class MG_Dropship_Block_Adminhtml_Sales_Order_View extends Mage_Adminhtml_Block_Sales_Order_View {
    public function  __construct() {

        parent::__construct();

        $this->_addButton('dropship', array(
            $message = "Are you sure you want to dropship?",
            'label'     => Mage::helper('Sales')->__('Dropship'),
            'onclick'   => "confirmSetLocation('{$message}','{$this->getUrl('MG_Dropship')}')"
        ));
    }
}

Any help would be greatly appreciated.

Upvotes: 0

Views: 3076

Answers (1)

seanbreeden
seanbreeden

Reputation: 6097

Everything in your module looks good. To get the additional function you want, you'll need to add the code below to override the Sales Order controller to handle the url.

In your config.xml set up the admin router like this:

<config>

...

   <admin>
        <routers>
           <adminhtml>
               <use>admin</use>
               <args>
                    <modules>
                        <dropship before="Mage_Adminhtml_Sales">MG_Dropship_Adminhtml</dropship>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>

...

</config>

Next, you'll need to set up your Adminhtml controller. This is what will answer the getUrl() that you defined in your onclick section of your button.

Create a file called OrderController.php and place it in app/code/local/MG/Dropship/controllers/Adminhtml/Sales/. Put the following code in the file:

include_once Mage::getModuleDir('controllers', 'Mage_Adminhtml') . DS . 'Sales' . DS . 'OrderController.php';  //  Some people use the full path but this is the most Magento-friendly way to do it.

class MG_Dropship_Adminhtml_Sales_OrderController extends Mage_Adminhtml_Sales_OrderController {

    public function dropshipAction() {

         //  Put all of your code for exporting and e-mailing your order here.
         //  You can use Mage::app()->getRequest()->getParam('order_id') to pull the order_id here.

         echo 'Your button works!';exit();  // This is just to test that your button actually works.  You should see a screen with this message and nothing else when you click the button.

    }

}

Now you'll need to change the getUrl() part of your button. It should be:

$this->_addButton('dropship', array(
        $message = "Are you sure you want to dropship?",
        'label'     => Mage::helper('Sales')->__('Dropship'),
        'onclick'   => "confirmSetLocation('{$message}','{$this->getUrl('*/*/dropship')}')"
    ));

Upvotes: 1

Related Questions