Grid Model – How to Change Value from Specific Collection

gridmodel

I can get from collection "sales/order" customer ids, but when customer is unregistered, value is "null". What can I do to show in grid which customer is registered, with filter "true" or "false". My code:

    protected function _prepareCollection()
    {    
        $collection = Mage::getModel('sales/order')->getCollection()
                        ->addAttributeToSelect('customer_id');
                foreach($collection as $coll)
                {
                    if ($coll['customer_id'] != '')
                    {
                        $coll['customer_id'] = 'yes';
                    }else{
                        $coll['customer_id'] = 'no';
                    }
                }
    ...

    protected function _prepareColumns()
    {
        $this->addColumn("customer_id", array(
                "header" => Mage::helper("mymodule")->__("Registered"),
                'type' => 'options',
                'options' => array(
                        'yes' => 'True',
                        'no' => 'False'
                    ),
                "index" => "customer_id",
            ));
     ...

This option was destroy grid filter. Is any better solution?

Best Answer

Instead of trying to force a display value based on customer id (which is pre-maturley loading the collection, having the result that your filters are not applied) you could instead add a new row to the grid based on customer_is_guest.

Something like this maybe.

protected function _prepareCollection()
{    
    $collection = Mage::getModel('sales/order')->getCollection()
                    ->addAttributeToSelect('customer_is_guest');
...

protected function _prepareColumns()
{
    $this->addColumn("customer_is_guest", array(
            "header" => Mage::helper("mymodule")->__("Is Guest"),
            'type' => 'options',
            'options' => array(
                    'yes' => 'True',
                    'no' => 'False'
                ),
            "index" => "customer_is_guest",
        ));
 ...
Related Topic