Mia
Mia

Reputation: 21

How to create filter_condition_callback on custom renderer in Magento admin grid?

I have added custom column to Customer Grid:

    $this->addColumn('shipping_name', array(
        'header'    => Mage::helper('customer')->__('Shipping name'),
        'index'     => 'entity_id',
        'renderer'  => new My_Unique_Block_Customer_Renderer_Shippingname(),
        'filter_condition_callback' => array($this, '_shippingNameFilter')
    ));

and the renderer Shippingname.php looks like:

class My_Unique_Block_Customer_Renderer_Shippingname extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract {

public function render(Varien_Object $row)
{

    $id = $row->getData($this->getColumn()->getIndex());

    $customer = Mage::getModel('customer/customer')->load($id); //customer id

    $data = "";

    if ( $customer->getDefaultShippingAddress() != null ) {
        $shipping_address = $customer->getDefaultShippingAddress();

        if ( $shipping_address->getFirstname() != null ) {
            $data .= $shipping_address->getFirstname();
        }

        if ( $shipping_address->getLastname() != null ) {

            if ( $shipping_address->getFirstname() != null ) {
                $data .= " ";
            }

            $data .= $shipping_address->getLastname();
        }

    }

    return $data;
}}

What should I substitute to function _shippingNameFilter() instead of "shipping_name like ?" to get filter in this column to work?

    protected function _shippingNameFilter($collection, $column)
{
    if (!$value = $column->getFilter()->getValue()) {
        return $this;
    }
    $this->getCollection()->getSelect()->where("shipping_name like ?", "%$value%");

    return $this;
}

Thanks!

Upvotes: 2

Views: 9029

Answers (2)

Kazim Noorani
Kazim Noorani

Reputation: 323

Yes. @Vishwas Soni is right. Further if one wants to join eav_attribute_option_value table to filtering option value, one can use below code.

$this->addColumn(
        'product_manufacturer',
        [
            'header' => __('Manufacturer'),
            'index' => 'manufacturer',
            'filter_condition_callback' => [$this, 'filterCustomManufacturer'],
            'header_css_class' => 'col-manufacturer',
            'column_css_class' => 'col-manufacturer'
        ]
    );

Below add filter condition callback function including joins like,

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

    $collection->getSelect()->joinLeft(array('c_p_e_i'=>'catalog_product_entity_int'),
    'e.row_id = c_p_e_i.row_id',array('value'));

    $collection->getSelect()->joinLeft(array('attribute_option'=>'eav_attribute_option_value'),
    'c_p_e_i.value = attribute_option.option_id and attribute_option.store_id = 0',
     array('eaov_val'=>'value' , 'eaov_option'=>'option_id'));

     $collection->getSelect()->where("`attribute_option`.`value` like ?", "%$value%");

    return $this;
}

Upvotes: 1

Vishwas Soni
Vishwas Soni

Reputation: 467

If I understood your question correct then for the custom filter you need to follow the below steps.

Here is your Customer Grid:

$this->addColumn('shipping_name', array(
    'header'    => Mage::helper('customer')->__('Shipping name'),
    'index'     => 'entity_id',
    'renderer'  => new My_Unique_Block_Customer_Renderer_Shippingname(),
    'filter_condition_callback' => array($this, '_customShippingFilterCallBack')
));

And add a method like this,

protected function _customShippingFilterCallBack($collection, $column)
{
   //Put your logic here..!!
if (!$value = $column->getFilter()->getValue()) 
{
    return $this;
}
$this->getCollection()->getSelect()->where("shipping_name like ?", "%$value%");

return $this;
}

Note: It's a basic structure to create custom filter. You need to do some changes according to your need.

Upvotes: 6

Related Questions