在Magento中为客户网格添加列 - 数据不填充 [英] Adding column to customer grid in Magento - Data not populating

查看:167
本文介绍了在Magento中为客户网格添加列 - 数据不填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我已经设置了这样的模块: p>

config.xml

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

WACI / AdminHtmlExt / Block / Customer / Grid.php

 <?php 
/ **
* Adminhtml客户网格块
*
* @category WACI
* @package WACI_AdminhtmlExt
* @author
* /

类WACI_AdminHtmlExt_Block_Customer_Grid扩展Mage_Adminhtml_Block_Customer_Grid
{

保护函数_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 / phone','default_billing',null,'left')
- > joinAttribute('billing_region','customer_address / region','default_billing',null''left')
- > joinAttribute('billing_country_id','customer_address / country_id','default_billing' '剩下');

$ this-> setCollection($ collection);

return parent :: _ prepareCollection();
}


保护函数_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',
));

// ...其余的功能,删除几列...


返回父:: _ prepareColumns();
}
}

Customer_Id case是一个自定义属性(跟踪一个内部客户ID)...不知道如果我需要添加逻辑来呈现这个正确的?但是它在管理中的显示效果很好,否则。



我已经阅读了几篇文章,提到为这样的网格添加新字段的渲染器 - 但只是许多人根本不提。



不太确定从哪里去 -



干杯











更新



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

  class WACI_AdminHtmlExt_Block_Customer_Grid扩展Mage_Adminhtml_Block_Customer_Grid 
{

/ * protected function _prepareCollection()
{
$ collection = Mage :: getResourceModel('customer / customer_collection')
- > addNameToSelect()
- > addAttributeToSelect('email')
- > addAttributeToSelect('created_at')
- > addAttributeToSelect('group_id')
- > ; addAttributeToSelect( user_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 / phone','default_billing',null''left')
- > joinAttribute
- > 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()调用。



干杯

解决方案

:_prepareCollection()结合您扩展原始类的事实。通过在类之后调用父类,您将使用原始代替替换创建的集合对象。您实际需要调用的是您的重载类的父级,您可以这样做...

 保护功能_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 / phone','default_billing',null''left')
- > joinAttribute('billing_region','customer_address / region','default_bi lle',null,'left')
- > joinAttribute('billing_country_id','customer_address / country_id','default_billing',null,'left');

$ this-> setCollection($ collection);

return Mage_Adminhtml_Block_Widget_Grid :: _ prepareCollection();
}


Trying to add a column with a custom attribute to the admin customer grid in Magento v1.11

I've got this module set up like so:

config.xml

<?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, in this case is a custom attribute (tracking an internal customer id)... Not sure if I need to add logic to render this correctly? But its showing up in the admin just fine, otherwise.

I've read a couple articles that mention adding a renderer for new fields in grids like this - but just as many don't mention it at all.

Not quite sure where to go from here -

Cheers





update

Just to clarify for those needing this solution:

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;
    }
}

Is a what I finally landed up. Skipped the _prepareCollenctions() call.

Cheers

解决方案

The problem is your return statement calling parent::_prepareCollection() combined with the fact you are extending the original class. By calling the parent class after your class you are replacing the collection object you created with the original. What you actually need to call is the parent of the class your overloading, which you can do like so...

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();
}

这篇关于在Magento中为客户网格添加列 - 数据不填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆