3

尝试在 Magento v1.11 的管理客户网格中添加具有自定义属性的列

我已经像这样设置了这个模块:

配置文件

<?xml version="1.0"?>
<config>
    <modules>
        <WACI_AdminHtmlExt>
            <version>0.1.0</version>
        </WACI_AdminHtmlExt>
    </modules>
    <global>
        <blocks>
            <adminhtml>
                <rewrite>
                    <customer_grid>WACI_AdminHtmlExt_Block_Customer_Grid</customer_grid>
                </rewrite>
            </adminhtml>
        </blocks>
    </global>
</config>

WACI/AdminHtmlExt/Block/Customer/Grid.php

<?php
/**
 * Adminhtml customer grid block
 *
 * @category   WACI
 * @package    WACI_AdminhtmlExt
 * @author     
 */

class WACI_AdminHtmlExt_Block_Customer_Grid extends Mage_Adminhtml_Block_Customer_Grid
{

    protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel('customer/customer_collection')
            ->addNameToSelect()
            ->addAttributeToSelect('email')
            ->addAttributeToSelect('created_at')
            ->addAttributeToSelect('group_id')
            ->addAttributeToSelect('customer_id')
            ->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
            ->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
            ->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
            ->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
            ->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');

        $this->setCollection($collection);

        return parent::_prepareCollection();
    }


    protected function _prepareColumns()
    {
        $this->addColumn('entity_id', array(
            'header'    => Mage::helper('customer')->__('ID'),
            'width'     => '50px',
            'index'     => 'entity_id',
            'type'      => 'number',
        ));

       $this->addColumn('customer_id', array(
            'header'    => Mage::helper('customer')->__('Dynamics ID'),
            'width'     => '75px',
            'index'     => 'customer_id',
        ));

        //... rest of the function, removing a couple columns...


        return parent::_prepareColumns();
    }
}

Customer_Id,在这种情况下是一个自定义属性(跟踪内部客户 ID)......不确定我是否需要添加逻辑才能正确呈现它?但它在管理员中显示得很好,否则。

我读过几篇文章,提到在这样的网格中为新字段添加渲染器 - 但同样许多人根本没有提到它。

不太确定从这里去哪里-

干杯





更新

只是为了澄清那些需要这个解决方案的人:

class WACI_AdminHtmlExt_Block_Customer_Grid extends Mage_Adminhtml_Block_Customer_Grid
{

    /*protected function _prepareCollection()
        {
            $collection = Mage::getResourceModel('customer/customer_collection')
                ->addNameToSelect()
                ->addAttributeToSelect('email')
                ->addAttributeToSelect('created_at')
                ->addAttributeToSelect('group_id')
                ->addAttributeToSelect('customer_id')
                ->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
                ->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
                ->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
                ->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
                ->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');

            $this->setCollection($collection);

            return parent::_prepareCollection();
    }*/

    public function setCollection($collection)
    {
        $collection->addAttributeToSelect('customer_id');                    
        parent::setCollection($collection);
    }

    protected function _prepareColumns()
    {

        $this->addColumn('customer_id', array(
            'header'    => Mage::helper('customer')->__('Dynamics ID'),
            'width'     => '75px',
            'index'     => 'customer_id',
        ));

        $this->addColumnsOrder('customer_id','entity_id');

        parent::_prepareColumns();

        $this->removeColumn('billing_country_id');


        return $this;
    }
}

是我终于登陆了。跳过了_prepareCollenctions()通话。

干杯

4

2 回答 2

2

问题是您调用 parent::_prepareCollection() 的 return 语句与您正在扩展原始类的事实相结合。通过在您的类之后调用父类,您将使用原始对象替换您创建的集合对象。你真正需要调用的是你重载的类的父类,你可以这样做......

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel('customer/customer_collection')
        ->addNameToSelect()
        ->addAttributeToSelect('email')
        ->addAttributeToSelect('created_at')
        ->addAttributeToSelect('group_id')
        ->addAttributeToSelect('customer_id')
        ->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
        ->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
        ->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
        ->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
        ->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');

    $this->setCollection($collection);

    return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
}
于 2012-09-13T18:57:27.187 回答
0

抱歉,我第一次回答时因为换行而误读了代码块。

createBlock()您的方法似乎没有返回有效对象。

于 2012-09-13T17:56:35.513 回答