在发票网格中添加自定义列 - Magento 1

Adding Custom Column in Invoice Grid - Magento 1

在 Sales->Invoice->Invoice Grid 中,我添加了一列 Payment method。

在$collection中,我加入了sales_flat_order_payment table所以我可以拉取每个entity_id的方法并成功完成。当我回显查询并在 Mysql 中使用它时,它 returns 我想要的结果。

现在,我希望 mysql 的方法列在发票网格中可见。

这是我所做的。

我在这里遵循了一些步骤。 Add custom renderer for a custom column in Magento grid

这是我的代码。

在我的

app/code/core/Mage/Adminhtml/Block/Sales/Invoice/Grid.php

这是我的 $collection

$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->join( array('a'=> mgmx_sales_flat_order_payment), 'a.entity_id = main_table.entity_id', array('a.method'));
$this->setCollection($collection);
/*echo $collection->getSelect();die();*/
return parent::_prepareCollection();

然后在_prepareColumns

$this->addColumn('payment_mode', array(
        'header'    => Mage::helper('sales')->__('Payment Mode'),
        'index'     => 'method',
        'renderer'  => 'Mage_Adminhtml_Block_Catalog_Product_Renderer_Red',
    ));

然后我在 Renderer Directory

中创建一个 Renderer Directoy 和一个 Red.php

app/code/core/Mage/Adminhtml/Block/Sales/Invoice/Renderer

<?php 
class Mage_Adminhtml_Block_Catalog_Product_Renderer_Red extends 
  Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract { 
  public function render(Varien_Object $row) { 
    $value =  $row->getData($this->getColumn()->getIndex());
  return $value;
 }
}
?>

但是这个returns一个错误

Fatal error: Call to a member function setColumn() on boolean in /home/xxxxxxx/xxxx.xxxxxx.com/xxxxx/includes/src/Mage_Adminhtml_Block_Widget_Grid_Column.php on line 291

为什么添加新列时需要自定义渲染器?

如果您不需要任何自定义输出,请忽略渲染器:

$this->addColumn('payment_mode', array(
        'header'    => Mage::helper('sales')->__('Payment Mode'),
        'index'     => 'method',
    ));

要修复您的示例,您必须更改渲染器的命名空间

你的 class 路径是

app/code/core/Mage/Adminhtml/Block/Sales/Invoice/Renderer

但是你的class名字是

Mage_Adminhtml_Block_Catalog_Product_Renderer_Red

这永远无法与 magento 自动加载一起使用。您必须将 class 放入:

app/code/core/Mage/Adminhtml/Block/Catalog/Product/Renderer

(或完全更改 class 名称,无论您喜欢什么)