我正在尝试在Magento View Order页面上创建一个按钮,当我点击它时,它会使用订单中包含的信息和订单中的项目向某个供应商发送电子邮件。
我已成功创建模块,按钮,我可以单击按钮,它会显示警告类型消息。但我无法弄清楚如何让按钮执行动作。目前,该按钮只是一个URL。
这就是我所拥有的:
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')}')"
));
}
}
非常感谢任何帮助。
答案 0 :(得分:1)
模块中的所有内容都很好看。要获得所需的附加功能,您需要添加以下代码以覆盖销售订单控制器以处理该URL。
在config.xml
设置管理路由器,如下所示:
<config>
...
<admin>
<routers>
<adminhtml>
<use>admin</use>
<args>
<modules>
<dropship before="Mage_Adminhtml_Sales">MG_Dropship_Adminhtml</dropship>
</modules>
</args>
</adminhtml>
</routers>
</admin>
...
</config>
接下来,您需要设置Adminhtml控制器。这将回答您在按钮的onclick
部分中定义的getUrl()。
创建一个名为OrderController.php
的文件,并将其放在app/code/local/MG/Dropship/controllers/Adminhtml/Sales/
中。将以下代码放在文件中:
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.
}
}
现在您需要更改按钮的getUrl()
部分。它应该是:
$this->_addButton('dropship', array(
$message = "Are you sure you want to dropship?",
'label' => Mage::helper('Sales')->__('Dropship'),
'onclick' => "confirmSetLocation('{$message}','{$this->getUrl('*/*/dropship')}')"
));