订单查看页面打印pdf按钮magento

时间:2013-01-24 23:59:19

标签: php magento orders

我正在尝试在magento admin的主订单明细页面上添加打印订单按钮。客户的旧magento有订单本身的打印按钮(付费或未付款,而不是发票)。看看它曾经是什么样子:

orderbar

我在后端启用了模板提示,模板文件不包含该按钮。我已经浏览了几个核心文件...... 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'
            ));
        }
    }
}
?>

这仍然无效。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

按钮在Mage_Adminhtml_Block_Sales_Order_View :: __构造中创建,带有$ this-&gt; 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语句来检查它是否是正确的块...缺点是它为每个块提供了一个调用开销和一个if语句开销所以我不确定这是最好的解决方案,但它肯定是最不具侵入性的,所以我可能会使用它(在此事件的情况下,'data_object'应更改为'block' - 事件在Mage_Core_Block_Abstract :: toHtml中触发dispatchEvent行。)

修改

在你的问题更新之后我已经测试了你的模块,就像我在评论中警告的那样,问题是模块的名称 - 小写。

应用程序的/ etc /模块/ 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 /型号/ 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'
            ));
        }
    }
}
?>

请注意正确命名文件(注意大写字符)并复制代码,它应该有效。

答案 1 :(得分:0)

@Domen Vrankar略有改善

adminhtml_block_html_before只会在管理区域内发送,core_block_abstract_to_html_before将在管理员和前端发送

    <events>
        <adminhtml_block_html_before>
            <observers>
            <CaitlinHavener_PrintOrder>
                <class>CaitlinHavener_PrintOrder_Model_Observer</class>
                <method>orderPageButton</method>
                <type>model</type>
            </CaitlinHavener_PrintOrder>
            </observers>
        </adminhtml_block_html_before>
    </events>

如果另一个模块重写Mage_Adminhtml_Block_Sales_Order_View,那么get_class($block)将返回新的块类,例如MagePal_Guest2Customer_Block_Adminhtml_Sales_Order_View,因此您的按钮将不再显示

<?php
// Order View Page button    
class CaitlinHavener_PrintOrder_Model_Observer
{
    public function orderPageButton( Varien_Event_Observer $observer )
    {
      $block = $observer->getEvent()->getData( 'block' );

        if($block->getId() == 'sales_order_view' && $block->getRequest()->getControllerName() == 'sales_order')
        { 
          ....