Magento将自定义块添加到销售订单视图

时间:2017-06-02 13:04:53

标签: magento magento-1.9

我正在尝试将自定义块添加到Magento管理员的订单视图中。我已经按照this教程进行了操作,但似乎没有用。

应用程序的/ etc /模块/ Webspeaks_MyModule.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Webspeaks_MyModule>
            <active>true</active>
            <codePool>local</codePool>
        </Webspeaks_MyModule>
    </modules>
</config>

应用程序/代码/本地/ Webspeaks / MyModule的在/ etc / config.xml中

<?xml version="1.0"?>
<config>
    <adminhtml>
        <layout>
            <updates>
                <webspeaks_mymodule>
                    <file>mymodule.xml</file>
                </webspeaks_mymodule>
            </updates>
        </layout>
        <events>
            <core_block_abstract_to_html_after>
                <observers>
                    <mymodule_custom_order_view_info>
                        <class>mymodule/observer</class>
                        <method>getSalesOrderViewInfo</method>
                    </mymodule_custom_order_view_info>
                </observers>
            </core_block_abstract_to_html_after>
        </events>
    </adminhtml>
</config>

应用程序/代码/本地/ Webspeaks / MyModule的/型号/ Observer.php

<?php
class Webspeaks_MyModule_Model_Observer {

    // This function is called on core_block_abstract_to_html_after event
    // We will append our block to the html
    public function getSalesOrderViewInfo(Varien_Event_Observer $observer) {
        $block = $observer->getBlock();
        // layout name should be same as used in app/design/adminhtml/default/default/layout/mymodule.xml
        if (($block->getNameInLayout() == 'order_info') && ($child = $block->getChild('mymodule.order.info.custom.block'))) {
            $transport = $observer->getTransport();
            if ($transport) {
                $html = $transport->getHtml();
                $html .= $child->toHtml();
                $transport->setHtml($html);
            }
        }
    }
}

应用程序/设计/ adminhtml /默认/默认/布局/ mymodule.xml

<?xml version="1.0"?>
<layout version="1.0">
    <!-- Adding the block in sales/order/view page -->
    <adminhtml_sales_order_view>
        <!-- You can change the reference to whatever you like. Look ate layout/sales.xml for more options -->
        <!-- This should be same in Model/Observer.php::getSalesOrderViewInfo() -->
        <reference name="order_info">
            <block type="mymodule/adminhtml_sales_order_view_info_block" name="mymodule.order.info.custom.block" template="mymodule/custom.phtml" before="order_history" />
        </reference>
    </adminhtml_sales_order_view>
</layout>

应用程序/设计/ adminhtml /默认/默认/模板/ MyModule的/ custom.phtml

<?php $order = $this->getOrder() ?>
<div class="entry-edit box-left">
    <div class="entry-edit-head">
        <h4 class="icon-head"><?php echo $this->__('My Custom Block') ?></h4>
    </div>
    <fieldset>
        <div id="mymodule_custom_block">
            Special message
        </div>
    </fieldset>
</div>
<div class="clear"></div>

应用程序\代码\本地\ Webspeaks \ MyModule的\块\ Adminhtml \销售\订单\查看\信息\ Block.php

<?php
class Webspeaks_MyModule_Block_Adminhtml_Sales_Order_View_Info_Block extends Mage_Core_Block_Template {

    protected $order;

    public function getOrder() {
        if (is_null($this->order)) {
            if (Mage::registry('current_order')) {
                $order = Mage::registry('current_order');
            }
            elseif (Mage::registry('order')) {
                $order = Mage::registry('order');
            }
            else {
                $order = new Varien_Object();
            }
            $this->order = $order;
        }
        return $this->order;
    }
}

1 个答案:

答案 0 :(得分:0)

默认情况下,只有2个选项可通过普通xml添加。在内容之前,以及之后的内容。

<adminhtml_sales_order_view>
       <reference name="content">
           <block type="core/template" name="test-1" template="test/template.phtml" after="sales_order_edit"/>
           <block type="core/template" name="test-2" template="test/template.phtml" before="sales_order_edit"/>
       </reference>
   </adminhtml_sales_order_view>    

but those templates will be shown in all tabs.

to insert into several parts, you have to add your template like:

   <adminhtml_sales_order_view>
       <reference name="order_tab_info">
           <block type="core/template" name="test-1" template="test/template.phtml" after="sales_order_edit"/>
           <block type="core/template" name="test-2" template="test/template.phtml" before="sales_order_edit"/>
       </reference>
   </adminhtml_sales_order_view>    
Watch out for the changed reference name. 

In the corresponding template `app\design\adminhtml\default\default\template\sales\order\view\tab\info.phtml` you can place your block everywhere you want

   <div id="order-messages">
       <?php echo $this->getChildHtml('order_messages') ?>
   </div>
   <?php echo $this->getChildHtml('order_info') ?>
   <?php echo $this->getChildHtml('test-1') ?>

主要问题是,sales_order_view有点硬编码,所以它会保留结构。

希望有所帮助。

你也可以尝试这个。

您可以点击以下链接。http://sohelrana09.wordpress.com/2011/06/24/how-to-overwrite-sales-view-order-phtml-file-in-magento/