我的事件观察者有什么问题?

What's wrong with my event observer?

我对我的事件观察器进行了编码以将自定义列添加到我的销售订单网格中,但仍然没有在我网站的管理面板中显示该列?此代码用于自定义 magento 销售订单网格。这是我的代码:

app/code/community/Mjsi/Orderprint/etc/config.xml

<adminhtml>
 ---
  <events>
    <core_layout_block_create_after>
        <observers>
            <mjsi_orderprint_add_columns>
                <class>Mjsi_Orderprint_Model_Observer</class>
                <method>addColumnsToGrid</method>
            </mjsi_orderprint_add_columns>
        </observers>
    </core_layout_block_create_after>
    <core_block_abstract_prepare_layout_before>
        <observers>
            <orderprint_core_block_abstract_prepare_layout_before>
                <class>Mjsi_Orderprint_Model_Observer</class>
                <method>addMassAction</method>
            </orderprint_core_block_abstract_prepare_layout_before>
        </observers>
    </core_block_abstract_prepare_layout_before>
    <adminhtml_widget_container_html_before>
        <observers>
            <mjsi_orderprint>
                 <class>Mjsi_Orderprint_Model_Observer</class>
                 <method>adminhtmlWidgetContainerHtmlBefore</method>
            </mjsi_orderprint>
        </observers>
    </adminhtml_widget_container_html_before>
 <events>
 ---
</adminhtml>

app/code/community/Mjsi/Orderprint/Model/Observer.php

<?php

 class Mjsi_Orderprint_Model_Observer
 {
 /** 
 * Add new column to orders grid
 *
 * @param Varien_Event_Observer $observer
 * @return $this
 */
 public function addColumnsToGrid(Varien_Event_Observer $observer)
 {
$block = $observer->getEvent()->getBlock();

// Check whether the loaded block is the orders grid block
if (!($block instanceof Mage_Adminhtml_Block_Sales_Order_Grid)
    || $block->getNameInLayout() != 'sales_order.grid'
) {
    return $this;
}

// Add a new column rigth after the "Status" column
$block->addColumnAfter('mjsi_orderprint', [
    'header' => $block->__('Printed'),
    'width' => '80px',
    'filter' => false,
    'renderer' =>'Mjsi_Orderprint_Block_Adminhtml_Sales_Order_Renderer_Printed',
    //'index' => 'coupon_rule_name',
], 'status');

return $this;
}

public function addMassAction(Varien_Event_Observer $observer)
{
$block = $observer->getEvent()->getBlock();

if(get_class($block) =='Mage_Adminhtml_Block_Widget_Grid_Massaction'
    && $block->getRequest()->getControllerName() == 'sales_order')
{
    $block->addItem('massPrintSalesOrders_order', array(
        'label' => 'Print Sales Orders',
   'url' => Mage::app()->getStore()->getUrl('*/*/massPrintSalesOrders'),
    ));
}
}

public function adminhtmlWidgetContainerHtmlBefore($event) {

$block = $event->getBlock();
if ($block instanceof Mage_Adminhtml_Block_Sales_Order_View) {
    $block->addButton('order_print', array(
        'label'     => Mage::helper('sales')->__('Print Orders'),
        'onclick'   => 'setLocation(\'' . $block->getUrl('*/*/print') . '\')',
    ));           
}
}
}

好的,要向销售网格添加新列,您需要在配置中添加此事件:

<sales_order_grid_collection_load_before>
            <observers>
                <mjsi_orderprint_add_columns>
                    <model>your_model/observer</model>
                    <method>addGridColumnsToSelect</method>
                </mjsi_orderprint_add_columns>
            </observers>
</sales_order_grid_collection_load_before>

然后在观察者中,您需要在主集合中添加您的专栏,我从订单 table:

添加了一个自定义字段 orygin_type
public function addGridColumnsToSelect(Varien_Event_Observer $observer)
{
    $collection = $observer->getOrderGridCollection();
    $orderTable = Mage::getSingleton('core/resource')->getTableName('sales_flat_order');

    $collection->getSelect()
             ->joinLeft(        
                ['order_table' => $orderTable],
                "main_table.entity_id = order_table.entity_id",
                [
                    'origin_type' => 'order_table.origin_type',

                ]
            );

    return $this;
}

然后在你的布局 xml 中加载配置,如果你没有你需要添加 `

<adminhtml>
        <layout>
            <updates>
                <your_model>
                    <file>your_file.xml</file>
                </your_model>
            </updates>
        </layout>
</adminhtml>

并在 app\design\adminhtml\default\default\layout\your_file.xml 中添加:

<?xml version="1.0" encoding="UTF-8"?>
<layout>
<sales_order_grid_update_handle_origin_type>
        <reference name="sales_order.grid">
            <action method="addColumnAfter">
                <columnId>origin_type</columnId>
                <arguments module="sales" translate="header">
                    <header>Origin Type</header>
                    <index>origin_type</index>
                    <type>text</type>
                    <width>100</width>
                    <filter_index>order_table.origin_type</filter_index>
                </arguments>
                <after>base_subtotal</after>
            </action>
        </reference>
    </sales_order_grid_update_handle_origin_type>
</layout>

希望对您有所帮助!