1

I am trying to add a column for "trained_in" on the customer admin page inside my magento site.

I have the following code

    $collection = Mage::getResourceModel('customer/customer_collection')
        ->addNameToSelect()
        ->addAttributeToSelect('trained_in')
        ->addAttributeToSelect('*')
        ->addAttributeToSelect('created_at')
        ->addAttributeToSelect('group_id')
        ->joinAttribute('trained_in','')
        ->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);
    //code from Mage_Adminhtml_Block_Widget_Grid::_prepareCollection()
    //since calling parent::_prepareCollection will render the code above useless
    //and you cannot call in php parent::parent::_prepareCollection()
    if ($this->getCollection()) {

        $this->_preparePage();

        $columnId = $this->getParam($this->getVarNameSort(), $this->_defaultSort);
        $dir      = $this->getParam($this->getVarNameDir(), $this->_defaultDir);
        $filter   = $this->getParam($this->getVarNameFilter(), null);

        if (is_null($filter)) {
            $filter = $this->_defaultFilter;
        }

        if (is_string($filter)) {
            $data = $this->helper('adminhtml')->prepareFilterString($filter);
            $this->_setFilterValues($data);
        }
        else if ($filter && is_array($filter)) {
            $this->_setFilterValues($filter);
        }
        else if(0 !== sizeof($this->_defaultFilter)) {
            $this->_setFilterValues($this->_defaultFilter);
        }

        if (isset($this->_columns[$columnId]) && $this->_columns[$columnId]->getIndex()) {
            $dir = (strtolower($dir)=='desc') ? 'desc' : 'asc';
            $this->_columns[$columnId]->setDir($dir);
            $this->_setCollectionOrder($this->_columns[$columnId]);
        }

        if (!$this->_isExport) {
            $this->getCollection()->load();
            $this->_afterLoadCollection();
        }
    }

    return $this;
}

/**
 * override the _prepareColumns method to add a new column after the 'email' column
 * if you want the new column on a different position just change the 3rd parameter
 * of the addColumnAfter method to the id of your desired column
 */
protected function _prepareColumns(){
    $this->addColumnAfter('trained_in', array(
        'header'    => Mage::helper('customer')->__('Trained In'),
        'index'     => 'trained_in'
    ),'name');
    return parent::_prepareColumns();
}

}

However on the front end the column is showing as empty next to each customer but when i dump out the collection it shows ['trained_in'] => 6? A little context :

Trained in was added as a custom attribute inside of magento, and it is also a multiple select field i wish to display.

1 Answer 1

0

I already overwrite grid. So my overwrite grid looks like:

app/code/local/SR/MagentoCommunity/Block/Adminhtml/Customer/Grid.php

<?php

class SR_MagentoCommunity_Block_Adminhtml_Customer_Grid extends Mage_Adminhtml_Block_Customer_Grid
{
    /**
     * @param Mage_Customer_Model_Resource_Customer_Collection $collection
     */
    public function setCollection($collection)
    {
        $collection->addAttributeToSelect('trained_in');
        parent::setCollection($collection);
    }

    protected function _prepareColumns()
    {
        parent::_prepareColumns();

        $this->addColumnAfter('trained_in', array(
            'header'    => Mage::helper('customer')->__('Trained In'),
            'index'     => 'trained_in'
        ), 'customer_since');

        $this->sortColumnsByOrder();
        return $this;
    }
}

[Update]

How I created attribute:

app/code/local/SR/MagentoCommunity/sql/sr_magentocommunity_setup/upgrade-0.0.0.1-0.0.0.2.php

<?php
/* @var $installer SR_MagentoCommunity_Model_Resource_Setup */
$installer = $this;

$installer->startSetup();

$installer->addAttribute("customer", "trained_in",  array(
    "type"     => "varchar",
    "backend"  => "",
    "label"    => "Trained In",
    "input"    => "multiselect",
    "source"   => "sr_magentocommunity/customer_attribute_source_trainedin",
    "visible"  => true,
    "required" => false,
    "default" => "",
    "frontend" => "",
    "unique"     => false,
    "note"       => ""
));

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


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

app/code/local/SR/MagentoCommunity/Model/Customer/Attribute/Source/Trainedin.php

<?php

class SR_MagentoCommunity_Model_Customer_Attribute_Source_Trainedin extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
    /**
     * Retrieve all options array
     *
     * @return array
     */
    public function getAllOptions()
    {
        if (is_null($this->_options)) {
            $this->_options = array(

                array(
                    "label" => Mage::helper("eav")->__("Myoption-1"),
                    "value" =>  1
                ),

                array(
                    "label" => Mage::helper("eav")->__("Myoption-2"),
                    "value" =>  2
                ),

                array(
                    "label" => Mage::helper("eav")->__("Myoption-3"),
                    "value" =>  3
                ),

                array(
                    "label" => Mage::helper("eav")->__("Myoption-4"),
                    "value" =>  4
                ),

            );
        }
        return $this->_options;
    }
}
2
  • My trained in still shows as blank ? Commented Mar 7, 2019 at 15:50
  • Not sure why not working for you. I have created new attribute with fresh magento installation and working fine
    – Sohel Rana
    Commented Mar 7, 2019 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.