0
votes

In the admin area of Manage Customers.

I’ve added a new custom column, BUT it doesn’t filter and it is not sortable either asc / desc

Here is my code

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


$collection->getSelect()->columns(array(’CustomerStatus’ => new Zend_Db_Expr ("(SELECT
CustomerStatusFROM users WHERE MagentoID =e.entity_id)")));

$this->setCollection($collection);

return parent::_prepareCollection();
}

I’ve tried loads of different methods and adding _addColumnFilterToCollection but just can’t seem to get it to work…

    protected function _addColumnFilterToCollection($column)
        {


            if ($column->getId() == 'CustomerStatus' && $column->getFilter()->getValue()) {
            $val        = $column->getFilter()->getValue();


            $this->getCollection()->addFieldToFilter('CustomerStatus', array('like' => $val));

// $this->getCollection()->getSelect()->where('CustomerStatus= ?', $val);

        }

            return $this;



        }

Then this is how I've added my column within _preparecolumns function

$this->addColumn('CustomerStatus', array(
         'header'    => Mage::helper('customer')->__('C Status'),
         'width'     => '100px',
        'index'     => 'CustomerStatus',
        'filter_index' => 'CustomerStatus'
    ));

Any ideas?

1
Can you post your _prepareColumns() function? - seanbreeden
Yes ofcourse, I'll edit my post - Shane

1 Answers

0
votes

Instead of trying to make the column return CustomerStatus directly, you could use frame_callback to populate the column and filter_condition_fallback to search the column.

I haven't tested this for your exact conditions but I've used the same technique successfully in the past when pulling in data into an Admin Grid like this.

protected function _prepareCollection()
{

    $collection = Mage::getResourceModel('customer/customer_collection')
        ->addNameToSelect()
        ->addAttributeToSelect('email')
        ->addAttributeToSelect('created_at')
        ->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');

    $collection->getSelect()->columns(array('CustomerStatus' => new Zend_Db_Expr ("(SELECT CustomerStatus FROM users WHERE MagentoID = e.entity_id)")));

    $this->setCollection($collection);
    return parent::_prepareCollection();
}

...

and change your addColumn like this:

$this->addColumn('CustomerStatus', array(
     'header'    => Mage::helper('customer')->__('C Status'),
     'width'     => '100px',
     'index'     => 'entity_id',
     'frame_callback' => array($this, 'callback_customerstatus'), // This calls the "callback_customerstatus" function below
     'filter_condition_callback' => array($this, 'filter_customerstatus'), // This calls the "filter_customerstatus" function that will modify your $collection to make searching possible
));

...

Add this new function that is called by the frame_callback

public function callback_customerstatus($value, $row, $column, $isExport) {
    $entity_id = $value; // $value is passed from the Grid
    $sql = "SELECT `CustomerStatus` FROM `users` WHERE MagentoID=".$entity_id;
    $resource = Mage::getSingleton('core/resource');
    $readConnection = $resource->getConnection('core_read');
    $customer_status = $readConnection->fetchOne($sql); // Return one row with the selected value from "$sql" 

    return $customer_status;
}

...

Add a new function called filter_customerstatus():

public function filter_customerstatus($collection, $column) {
    if (!$value = $column->getFilter()->getValue()) {
        return $this;
    }

    $this->getCollection()->getSelect()->where(
        "CustomerStatus like ?"
        , "%$value%");

    return $this;
}

...

Notice the new call to filter_condition_callback that adds the where condition necessary to make the search function.