7

我正在尝试向销售订单网格添加一个自定义列,并且我已将连接和列添加到集合中,但该列显示为空。此外,当我尝试过滤时,我收到此错误:找不到列:1054 Unknown column 'method' in 'where Clause'. 我的代码似乎与大多数教程中的代码相同,所以我无法弄清楚为什么我的专栏没有任何付款数据。我回应了最终的 SQL 查询并在 MySQL Workbench 中运行它,结果和语法似乎都很好。我错过了什么吗?我需要在 sales_flat_order_grid 表中添加一列吗?我意识到当我进行过滤时,我的“漂亮”支付方式名称可能与我在列中显示的支付代码不匹配,但在过滤选项中使用支付代码时我遇到了同样的问题。我想在我得到一些列数据之后,我会稍后再打那个细节。我正在使用企业版 1.12.0.2。

app/code/local/mymodule/adminhtml/block/sales/order/grid:

     protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->_getCollectionClass());
    $collection->getSelect()->joinLeft(array('sfop'=>'sales_flat_order_payment'), 'main_table.entity_id = sfop.parent_id',array('sfop.method'));
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

     protected function _prepareColumns()
{  //edited for brevity...only my additions below![enter image description here][1]
         $payments = Mage::getSingleton('payment/config')->getActiveMethods();
    $methods = array();
    foreach ($payments as $paymentCode=>$paymentModel)
    {
        $paymentTitle = Mage::getStoreConfig('payment/'.$paymentCode.'/title');
        $methods[$paymentCode] = $paymentTitle;
    }

    $this->addColumn('method', array(
        'header' => Mage::helper('sales')->__('Payment Method'),
        'index' => 'method',
        'filter_index' => 'sfop.method',
        'type'  => 'options',
        'width' => '70px',
        'options' => $methods,
    ));

}

管理屏幕

4

4 回答 4

6

您需要返回 Admin Grid 的父级而不是您正在重写的类的父级。

将方法 _prepareCollection() 替换为以下内容:-

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->_getCollectionClass());
    $collection->join(array('payment'=>'sales/order_payment'),'main_table.entity_id=parent_id','method');
    $this->setCollection($collection);
    return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
}
于 2013-07-27T10:06:38.983 回答
3

中提出的解决方案:

http://www.atwix.com/magento/column-to-orders-grid/

干净多了。

无论如何,如果在网站范围内启用了付款方式,则将 $methods 作为后端网格选项检索的方式不是 Multistore-ready。在这种情况下,请:

        $methods = array();
        foreach (Mage::app()->getStores() as $storeId => $store) {
        $payments = Mage::getSingleton('payment/config')->getActiveMethods($storeId);

        foreach ($payments as $paymentCode => $paymentModel) {
            $paymentTitle = Mage::getStoreConfig('payment/'.$paymentCode.'/title');
            if (!isset($methods[$paymentCode])) {
                $methods[$paymentCode] = $paymentTitle;
            }
        }
    }
于 2015-01-22T15:01:48.423 回答
2
$collection->getSelect()->joinLeft('sales_flat_order_payment', 'main_table.entity_id = sales_flat_order_payment.parent_id',array('method'));
$this->addColumn('method', array(
        'header' => Mage::helper('sales')->__('Method'),
        'index' => 'method',
        'filter_index' => 'sales_flat_order_payment.method',
        ));
于 2014-12-29T12:11:56.960 回答
1

对于渲染,并非所有付款方式都适用于标题(例如 TIG PostNL)

$payments = Mage::getSingleton('payment/config')->getActiveMethods();
$methods = array();
foreach ($payments as $paymentCode=>$paymentModel)
{
    $paymentTitle = Mage::getStoreConfig('payment/'.$paymentCode.'/title', 1);
    if(empty($paymentTitle)) {
        $paymentTitle = Mage::helper('payment')->getMethodInstance($paymentCode)->getTitle();
    }
    $methods[$paymentCode] = $paymentTitle;
}
于 2017-01-06T11:06:25.573 回答