I have created a custom customer attribute:

$installer->addAttribute('customer', 'forum_username', array(
    'type' => 'varchar',
    'input' => 'text',
    'label' => 'Forum username',
    'global' => 1,
    'visible' => 1,
    'required' => 0,
    'user_defined' => 1,
    'default' => '',
    'visible_on_front' => 1,
));

Now i want to show it on customer grid I have extended Mage_Adminhtml_Block_Customer_Grid and addet a column:

protected function _prepareColumns()
{
    $this->addColumnAfter('forum_username', array(
        'header'    => Mage::helper('customization')->__('Forum user name'),
        'index'     => 'forum_username',
        'type'    => 'text'
    ),'email');
    return parent::_prepareColumns();
}

The column shows in the grid but i cant get the attribute value, i don't understand how to get the value with joinAttribute() as to get the postcode number:

->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')

I have tried to understand how it works on this page: http://magentocommerces.wordpress.com/2012/04/17/addattributetosortaddexpressionattributetoselect-joinattribute/ and come with this:

->joinAttribute('forum_username', 'customer/forum_username', 'attribute_set_id', null, 'left')

but it doesn't works

有帮助吗?

解决方案

Never84,Please add this code addAttributeToSelect('forum_username'),Grid like this-

  protected function _prepareCollection()
                {
                    $collection = Mage::getResourceModel('customer/customer_collection')
                        ->addNameToSelect()
                        ->addAttributeToSelect('email')
                        ->addAttributeToSelect('created_at')
        ->addAttributeToSelect('forum_username')
                        ->addAttributeToSelect('group_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();
   return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
                }



protected function _prepareColumns()
    {
        $this->addColumn('entity_id', array(
            'header'    => Mage::helper('customer')->__('ID'),
            'width'     => '50px',
            'index'     => 'entity_id',
            'type'  => 'number',
        ));
        /*$this->addColumn('firstname', array(
            'header'    => Mage::helper('customer')->__('First Name'),
            'index'     => 'firstname'
        ));
        $this->addColumn('lastname', array(
            'header'    => Mage::helper('customer')->__('Last Name'),
            'index'     => 'lastname'
        ));*/
        $this->addColumn('name', array(
            'header'    => Mage::helper('customer')->__('Name'),
            'index'     => 'name'
        ));
        $this->addColumn('email', array(
            'header'    => Mage::helper('customer')->__('Email'),
            'width'     => '150',
            'index'     => 'email'
        ));
        $this->addColumn('forum_username', array(
            'header'    => Mage::helper('customer')->__('Forum user name'),
            'width'     => '50px',
            'index'     => 'forum_username',
            'type'  => 'text',
        ));

        $groups = Mage::getResourceModel('customer/group_collection')
            ->addFieldToFilter('customer_group_id', array('gt'=> 0))
            ->load()
            ->toOptionHash();



     $this->addColumn('group', array(
            'header'    =>  Mage::helper('customer')->__('Group'),
            'width'     =>  '100',
            'index'     =>  'group_id',
            'type'      =>  'options',
            'options'   =>  $groups,
        ));

        $this->addColumn('Telephone', array(
            'header'    => Mage::helper('customer')->__('Telephone'),
            'width'     => '100',
            'index'     => 'billing_telephone'
        ));

        $this->addColumn('billing_postcode', array(
            'header'    => Mage::helper('customer')->__('ZIP'),
            'width'     => '90',
            'index'     => 'billing_postcode',
        ));

        $this->addColumn('billing_country_id', array(
            'header'    => Mage::helper('customer')->__('Country'),
            'width'     => '100',
            'type'      => 'country',
            'index'     => 'billing_country_id',
        ));

        $this->addColumn('billing_region', array(
            'header'    => Mage::helper('customer')->__('State/Province'),
            'width'     => '100',
            'index'     => 'billing_region',
        ));

        $this->addColumn('customer_since', array(
            'header'    => Mage::helper('customer')->__('Customer Since'),
            'type'      => 'datetime',
            'align'     => 'center',
            'index'     => 'created_at',
            'gmtoffset' => true
        ));

        if (!Mage::app()->isSingleStoreMode()) {
            $this->addColumn('website_id', array(
                'header'    => Mage::helper('customer')->__('Website'),
                'align'     => 'center',
                'width'     => '80px',
                'type'      => 'options',
                'options'   => Mage::getSingleton('adminhtml/system_store')->getWebsiteOptionHash(true),
                'index'     => 'website_id',
            ));
        }

        $this->addColumn('action',
            array(
                'header'    =>  Mage::helper('customer')->__('Action'),
                'width'     => '100',
                'type'      => 'action',
                'getter'    => 'getId',
                'actions'   => array(
                    array(
                        'caption'   => Mage::helper('customer')->__('Edit'),
                        'url'       => array('base'=> '*/*/edit'),
                        'field'     => 'id'
                    )
                ),
                'filter'    => false,
                'sortable'  => false,
                'index'     => 'stores',
                'is_system' => true,
        ));

        $this->addExportType('*/*/exportCsv', Mage::helper('customer')->__('CSV'));
        $this->addExportType('*/*/exportXml', Mage::helper('customer')->__('Excel XML'));
        return parent::_prepareColumns();
    }

Also Installer like this ---

 <?php
    $installer = $this;
    $installer->startSetup();

    /*Create Artist of the month */
    $installer->addAttribute('customer', 'forum_username', array(
        'input' => 'text',
        'type' => 'varchar',
        'label' => 'Forum Username',
        'visible' => 1,
        'required' => 0,
        'user_defined' => 1,
    ));


    $attribute   = Mage::getSingleton("eav/config")->getAttribute("customer", "forum_username");
    $used_in_forms=

array();

$used_in_forms[]="adminhtml_customer";
        $attribute->setData("used_in_forms", $used_in_forms)
        ->setData("is_used_for_customer_segment", true)
        ->setData("is_system", 0)
        ->setData("is_user_defined", 1)
        ->setData("is_visible", 1)
        ->setData("sort_order", 100)
        ;
        $attribute->save();

$installer->endSetup();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top