我制作了一个简单的模块,向Sales > Order Grid添加额外的字段。它工作得很好,但问题是我们正在使用<rewrites>
订单网格的另一个模块将他们的字段添加到网格中。我想尝试将其更改为使用事件(此外,这是一种更好的方法)...
问题是我一直在尝试和尝试,但我无法实现它。(也许我还不能掌握事件/观察者的概念......)
这是我当前的模块结构:
app/code/local/Artizara/OrderGridAdditions/
app/code/local/Artizara/OrderGridAdditions/Block/Sales/Order/Grid.php
app/code/local/Artizara/OrderGridAdditions/etc/config.xml
app/code/local/Artizara/OrderGridAdditions/controllers [empty]
app/code/local/Artizara/OrderGridAdditions/Helper [empty]
app/code/local/Artizara/OrderGridAdditions/Model [empty]
在我的Grid.php文件中,我已将主 Grid.php 文件内容复制到我的 Grid.php 文件中并编辑了_getCollectionClass()
,_prepareCollection()
和_prepareColumns()
函数。
我已将其更改_getCollectionClass()
为:
//return 'sales/order_grid_collection';
return 'sales/order_collection';
我已将其更改_prepareCollection()
为:
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->joinLeft(array('sfog' => 'sales_flat_order_grid'),'main_table.entity_id = sfog.entity_id',array('sfog.shipping_name','sfog.billing_name'));
$collection->getSelect()->joinLeft(array('sfo'=>'sales_flat_order'),'sfo.entity_id=main_table.entity_id',array('sfo.customer_email','sfo.weight','sfo.discount_description','sfo.increment_id','sfo.store_id','sfo.created_at','sfo.status','sfo.base_grand_total','sfo.grand_total','shipping_description','sfo.total_item_count'));
$collection->getSelect()->joinLeft(array('sfoa'=>'sales_flat_order_address'),'main_table.entity_id = sfoa.parent_id AND sfoa.address_type="shipping"',array('sfoa.street','sfoa.city','sfoa.region','sfoa.postcode','sfoa.telephone'));
//$collection->getSelect()->joinLeft(array('sfop' => 'sales_flat_order_payment'),'main_table.entity_id = sfop.entity_id',array('sfop.method'));
$this->setCollection($collection);
return parent::_prepareCollection();
我已经添加了_prepareColumns()
这样的列(这里只添加了一个作为示例):
$this->addColumn('total_item_count', array(
'header' => Mage::helper('sales')->__('Total Items'),
'index' => 'total_item_count',
'filter_index' => 'sfo.total_item_count',
'width' => '50px',
));
在我的config.xml
文件中,我有一个简单的<rewrite>
:
<modules>
<Artizara_OrderGridAdditions>
<version>0.1.0</version>
</Artizara_OrderGridAdditions>
</modules>
<global>
<blocks>
<adminhtml>
<rewrite>
<sales_order_grid>Artizara_OrderGridAdditions_Block_Sales_Order_Grid</sales_order_grid>
</rewrite>
</adminhtml>
<ordergridadditions>
<class>Artizara_OrderGridAdditions_Block</class>
</ordergridadditions>
</blocks>
</global>
将其更改为事件/观察者
我一直在尝试将其更改为使用事件但遇到困难的模块。我一直在尝试关注这里的其他一些答案(那些不是重写的,但他们使用不同的方法将自定义表从数据库添加到网格中)。我正在寻找使用我已经拥有的相同设置。
我添加/Helper/Data.php
了包含以下内容的容器:
class Artizara_OrderGridAdditions_Helper_Data extends Mage_Core_Helper_Abstract{ }
在/etc/config.xml
我尝试了很多东西。这是我最近的尝试:
<modules>
<Artizara_OrderGridAdditions>
<version>0.1.0</version>
</Artizara_OrderGridAdditions>
</modules>
<adminhtml>
<events>
<adminhtml_block_html_before>
<observers>
<Artizara_OrderGridAdditions_Observer>
<class>Artizara_OrderGridAdditions_Model_Observer</class>
<method>addAdditionsToGrid</method>
</Artizara_OrderGridAdditions_Observer>
</observers>
</adminhtml_block_html_before>
</events>
</adminhtml>
<global>
<models>
<Artizara_OrderGridAdditions>
<class>Artizara_OrderGridAdditions_Model</class>
</Artizara_OrderGridAdditions>
</models>
<blocks>
<Artizara_OrderGridAdditions>
<class>Artizara_OrderGridAdditions_Block</class>
</Artizara_OrderGridAdditions>
</blocks>
<helper>
<Artizara_OrderGridAdditions>
<class>Artizara_OrderGridAdditions_Helper</class>
</Artizara_OrderGridAdditions>
</helper>
</global>
然后在/Model/Observer.php
我有:
class Artizara_OrderGridAdditions_Model_Observer {
public function addAdditionsToGrid(Varien_Event_Observer $observer) {
// code here
}
}
在里面addAdditionsToGrid()
,我尝试了很多不同的方法,包括复制整个Grid.php
文件,但似乎没有任何效果(错误):(
请帮助指导我使用事件重新制作这个简单的模块!