我正在尝试在 magento 管理的主订单详细信息页面上添加打印订单按钮。客户的旧 magento 有一个订单本身的打印按钮(已付款或未付款,不是发票)。看看它以前是什么样子:
我已在后端启用模板提示,但模板文件不包含该按钮。我已经浏览了几个核心文件... html 如下所示。我如何将其转换为适用于当前查看的订单的 php 按钮?
<button id="id_d808fbd2d533d4e7b8e4a3fcd6274251" title="Back" type="button" class="scalable back" onclick="setLocation('http://test.animalnecessity.com/index.php/admin/sales_order/index/order_id/15852/key/28a65aa166da1664c65971decf3e472c/')" style="">
我可以实现这个吗? Magento - Add Button to Sales Order View Page (Observer/Event)如果是这样我应该把这段代码放在哪里?
我将其设置为典型的模块结构,如下所述:http://alanstorm.com/magento_config 。我的etc文件夹中的config.xml有以下内容
<config>
<modules>
<CaitlinHavener_printOrder>
<version>0.1.0</version>
</CaitlinHavener_printOrder>
</modules>
<global>
<events>
<core_block_abstract_to_html_before>
<observers>
<CaitlinHavener_printOrder>
<class>CaitlinHavener_printOrder_Model_Observer</class>
<method>orderPageButton</method>
<type>model</type>
</CaitlinHavener_printOrder>
</observers>
</core_block_abstract_to_html_before>
</events>
</global>
</config>
我的 CaitlinHavener_printOrder.xml 以下内容
<config>
<modules>
<CaitlinHavener_printOrder>
<active>true</active>
<codePool>local</codePool>
</CaitlinHavener_printOrder>
</modules>
</config>
和Observer.php
<?php
// Order View Page button
class CaitlinHavener_printOrder_Model_Observer
{
public function orderPageButton( Varien_Event_Observer $observer )
{
if(get_class($block) =='Mage_Adminhtml_Block_Sales_Order_View'
&& $block->getRequest()->getControllerName() == 'sales_order')
{
$block->addButton('test_print', array(
'label' => 'Test',
'onclick' => 'setLocation(\'' . $block->getUrl('html/sales_order/print') . '\')',
'class' => 'go'
));
}
}
}
?>
这仍然不起作用。有什么想法吗?
最佳答案
这些按钮是在 Mage_Adminhtml_Block_Sales_Order_View::__construct 中通过 $this->addButton 函数调用创建的。
至于事件,您可以使用其中一个,但我无法从头顶上记忆起哪一个(如果有的话)适合调用。
要列出所有触发的事件,您可以使用 Mage::log( $name ); 添加日志记录到 Mage::dispatchEvent 函数中
使用该名称,您可以在 config.xml 中声明监听器(在 config/global/events 标记路径下):
<name_that_was_printed_by_the_above_function>
<observers>
<YourModuleNamespace_YourModuleName>
<class>moduleName/observer</class>
<method>functionName</method>
<type>model</type>
</YourModuleNamespace_YourModuleName>
</observers>
</name_that_was_printed_by_the_above_function>
然后创建一个模块类Observer.php
class Namespace_ModuleName_Model_Observer
{
public function functionName( Varien_Event_Observer $observer )
{
$block = $observer->getEvent()->getData( 'data_object' ); // this line may vary
$block->addButton( ...stuff here... // take for reference the answer from the question you linked to
return $this;
}
}
但就像我说的,可能没有一个观察者能满足您的需求,您将不得不寻找另一个更具侵入性的解决方案......
编辑
您可能必须使用 core_block_abstract_to_html_before 并使用 if 语句来检查它是否是正确的 block ...缺点是它会为每个 block 提供调用开销和 if 语句开销,所以我不确定是否这是最好的解决方案,但它肯定是干扰最小的,所以我可能会使用它(如果发生此事件,“data_object”应更改为“block” - 事件在 Mage_Core_Block_Abstract::toHtml 的dispatchEvent行中触发)。
编辑
在您的问题更新后,我测试了您的模块,就像我在评论中警告的那样,问题出在您的模块名称 - 小写。
应用程序/etc/modules/CaitlinHavener_PrintOrder.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<CaitlinHavener_PrintOrder>
<active>true</active>
<codePool>local</codePool>
</CaitlinHavener_PrintOrder>
</modules>
</config>
应用程序/代码/本地/CaitlinHavener/PrintOrder/etc/config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<CaitlinHavener_PrintOrder>
<version>0.1.0</version>
</CaitlinHavener_PrintOrder>
</modules>
<global>
<events>
<core_block_abstract_to_html_before>
<observers>
<CaitlinHavener_PrintOrder>
<class>CaitlinHavener_PrintOrder_Model_Observer</class>
<method>orderPageButton</method>
<type>model</type>
</CaitlinHavener_PrintOrder>
</observers>
</core_block_abstract_to_html_before>
</events>
</global>
</config>
本地/CaitlinHavener/PrintOrder/Model/Observer.php
<?php
// Order View Page button
class CaitlinHavener_PrintOrder_Model_Observer
{
public function orderPageButton( Varien_Event_Observer $observer )
{
$block = $observer->getEvent()->getData( 'block' );
if(get_class($block) =='Mage_Adminhtml_Block_Sales_Order_View'
&& $block->getRequest()->getControllerName() == 'sales_order')
{
$block->addButton('test_print', array(
'label' => 'Test',
'onclick' => 'setLocation(\'' . $block->getUrl('html/sales_order/print') . '\')',
'class' => 'go'
));
}
}
}
?>
请注意正确命名文件(注意大写字符)并复制代码,它应该可以工作。
关于php - 订单查看页面打印 pdf 按钮 magento,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14513085/