我一直试图添加"状态"查看客户信息时显示的订单网格中的列 - >订单标签。
我已经能够添加列,并且过滤器会填充正确的状态值,但它会中断默认排序,您无法按任何项目或排序进行过滤。
我知道这可能是一个更好的添加列的方法,但是我发现的所有示例都与主Orders表相关,我无法弄清楚如何为这个用例修改它。
这是我的扩展程序代码 - 在/Pnp/Customer/Block/Customer/Edit/Tab/Orders.php
<?php
class Pnp_Customer_Block_Customer_Edit_Tab_Orders extends Mage_Adminhtml_Block_Customer_Edit_Tab_Orders
{
public function __construct()
{
parent::__construct();
$this->setId('customer_orders_grid');
$this->setUseAjax(true);
$this->setDefaultSort('created_at');
$this->setDefaultDir('DESC');
$this->setSaveParametersInSession(true);
}
protected function _prepareCollection()
{
$collection = Mage::getResourceModel('sales/order_grid_collection')
->addFieldToSelect('entity_id')
->addFieldToSelect('increment_id')
->addFieldToSelect('customer_id')
->addFieldToSelect('created_at')
->addFieldToSelect('grand_total')
->addFieldToSelect('order_currency_code')
->addFieldToSelect('store_id')
->addFieldToSelect('billing_name')
->addFieldToSelect('shipping_name')
->addFieldToSelect('status')
->addFieldToFilter('customer_id', Mage::registry('current_customer')->getId())
->setIsCustomerMode(true);
$this->setCollection($collection);
return $this;
}
protected function _prepareColumns()
{
parent::_prepareColumns();
$this->addColumn('status', array(
'header' => Mage::helper('sales')->__('Status'),
'index' => 'status',
'type' => 'options',
'width' => '70px',
'options' => Mage::getSingleton('sales/order_config')->getStatuses()
));
return parent::_prepareColumns();
}
}
提前感谢您提供的任何帮助!
非常感谢到目前为止的答案。
我几乎已经将Harit和Emipro Technologies的答案结合在了一起,这是我目前所拥有的:
等/ config.xml中
<?xml version="1.0"?>
<config>
<modules>
<Pnp_Customer>
<version>0.4.0</version>
</Pnp_Customer>
</modules>
<global>
<models>
<pnp_statusgrid>
<class>Pnp_Customer_Model</class>
</Pnp_statusgrid>
</models>
</global>
<adminhtml>
<events>
<core_block_abstract_prepare_layout_before>
<observers>
<statusgrid_column_append>
<type>model</type>
<class>Pnp_Customer_Model_Observer</class>
<method>appendCustomColumn</method>
</statusgrid_column_append>
</observers>
</core_block_abstract_prepare_layout_before>
</events>
</adminhtml>
</config>
型号/ Observer.php
<?php
class Pnp_Customer_Model_Observer extends Varien_Event_Observer
{
/**
* Adds column to admin customers grid
*
* @param Varien_Event_Observer $observer
* @return Pnp_Customer_Model_Observer
*/
public function appendCustomColumn(Varien_Event_Observer $observer)
{
$block = $observer->getBlock();
if (!isset($block)) {
return $this;
}
if ($block->getType() == 'adminhtml/customer_edit_tab_orders') {
/* @var $block Mage_Adminhtml_Block_Customer_Grid */
$block->addColumn('status', array(
'header' => Mage::helper('customer')->__('Status'),
'type' => 'options',
'options' => Mage::getSingleton('sales/order_config')->getStatuses(),
'width' => '70px',
'index' => 'status',
'renderer' => 'Pnp_Customer_Block_Customer_Edit_Tab_Renderer_Status',
}
}
}
块/客户/编辑/标签/ Orders.php
<?php
class Pnp_Customer_Block_Customer_Edit_Tab_Orders extends Mage_Adminhtml_Block_Customer_Edit_Tab_Orders
{
protected function _prepareCollection()
{
$collection = Mage::getResourceModel('sales/order_grid_collection')
->addFieldToSelect('entity_id')
->addFieldToSelect('increment_id')
->addFieldToSelect('customer_id')
->addFieldToSelect('created_at')
->addFieldToSelect('grand_total')
->addFieldToSelect('order_currency_code')
->addFieldToSelect('store_id')
->addFieldToSelect('billing_name')
->addFieldToSelect('shipping_name')
->addFieldToSelect('status')
->addFieldToFilter('customer_id', Mage::registry('current_customer')->getId())
->setIsCustomerMode(true);
$this->setCollection($collection);
return $this;
}
}
块/客户/编辑/标签/渲染器/ status.php
<?php
class Pnp_Customer_Block_Customer_Edit_Tab_Renderer_Status extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract {
public function render(Varien_Object $row)
{
if (!$value = $this->getColumn()->getIndex()){
return $this;
}
$value = $row->getData('increment_id');
$order = Mage::getModel('sales/order')->loadByIncrementId($value);
return '<span >'.$order['status']'</span>';
}
}
?>
除了列显示状态名称为underscored(即payment_pending)外,一切正常。我查看了返回的对象,无法看到标签版本。最后完成上述操作,是否有人能指出我正确的方向来打印属性的人类可读版本?
最终编辑:
我设法解决了 - 我改变了:
return '<span >'.$order['status']'</span>';
到
return '<span >'.$order->getStatusLabel().'</span>';
答案 0 :(得分:1)
这绝对适用于添加渲染器
按
更改列$this->addColumn('status', array(
'header' => Mage::helper('customer')->__('Status'),
'index' => 'status',
'type' => 'options',
'options' => $this->_getorderstatus(),
'renderer' => 'Pnp_Customer_Block_Customer_Edit_Renderer_Status',
));
在同一档案中添加新功能
protected function _getorderstatus(){
$ordstatus = (array) Mage::getModel('sales/order_status')->getResourceCollection()->getData();
$orderstatus=array();
foreach($ordstatus as $item){
$orderstatus[$item['status']]=$item['label'];
}
return $orderstatus;
}
status.php 把功能放到
public function render(Varien_Object $row)
{
if (!$value = $this->getColumn()->getIndex()){
return $this;
}
$value = $row->getData('increment_id');
$order = Mage::getModel('sales/order')->loadByIncrementId($value);
return '<span >'.$order['status'].'</span>';
}
答案 1 :(得分:1)
实现这一目标的一种更简单可靠的方法是通过观察员。
创建模块
在 app / code / local / My / Module / etc / config.xml
<global> <models> <my_customgrid> <class>My_Module_Model</class> </my_customgrid> </models> </global> <adminhtml> <events> <core_block_abstract_prepare_layout_before> <observers> <customgrid_column_append> <type>model</type> <class>My_Module_Model_Observer</class> <method>appendCustomColumn</method> </customgrid_column_append> </observers> </core_block_abstract_prepare_layout_before> </events> </adminhtml>
它的作用是,它允许您在准备布局之前观察块。
为您的模块创建一个名为Observer.php的文件,并记下以下内容:
class My_Module_Model_Observer扩展Varien_Event_Observer {
/**
*
* @param Varien_Event_Observer $observer
* @return \My_Module_Model_Observer
*/
public function appendCustomColumn(Varien_Event_Observer $observer) {
$block = $observer->getBlock();
if (!isset($block)) {
return $this;
}
if ($block->getType() == 'adminhtml/customer_grid') {
/* @var $block Mage_Adminhtml_Block_Customer_Grid */
$block->addColumnAfter('field', array(
'header' => 'Your Field',
'type' => 'text',
'index' => 'field',
), 'email');
}
}
}
请点击此链接获取更多信息: http://www.atwix.com/magento/add-column-to-customers-grid-alternative-way/
希望它有所帮助。