Magento Grid Filter all Entries with no Value

Magento Grid Filter all Entries with no Value

October 15, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Grid Filter all Entries with no Value

Filtering Magento Customer Grid for Parent Accounts

Hello developers! Today we’re going to tackle an interesting challenge involving customer accounts in Magento. We’ll explore how to filter the grid overview to show only parent accounts or subaccounts based on the value of a custom field called “parent_customer”.

The Challenge: Filtering Empty Values

The core issue here is filtering for items that don’t have a specific attribute set. In our case, we want to find all customer records where the “parent_customer” field is empty (not set). However, standard Magento filter callbacks expect a value to filter against.

Let’s dive into the provided code and understand what’s happening:

$this->addColumn('parent_customer', array(
    'header' => Mage::helper('customer')->__('Subaccount'),
    'width' => '10',
    'type' => 'options',
    'options' => array('0' => 'Nein', '1' => 'Ja'),
    'index' => 'parent_customer',
    'filter_condition_callback' => array($this, '_callbackParentCustomer'),
));

This snippet adds a new column to the customer grid with options for ‘Yes’ and ‘No’. The _callbackParentCustomer method is called when filtering this field.

The Callback Method: Filtering Logic

The callback method looks like this:

protected function _callbackParentCustomer($collection, $column)
{
    if ($value != 0) {
        Mage::log('show subaccounts', null, 'product.log', true);
        $collection->addFieldToFilter('parent_customer', array('gt' => 0));
    } else {
        Mage::log('hide subaccounts', null, 'product.log', true);
        $collection->addFieldToFilter('parent_customer', array('eq' => ''));
    }

    return $this;
}

When the filter value is not set (null), it simply returns without applying any filters. When a value is provided:

  • If it’s 1, it adds a filter to show only subaccounts (gt means ‘greater than’) with parent_customer > 0.
  • If it’s 0, it attempts to add a filter for parent accounts by setting the field to an empty string (eq means ‘equals’).

The Issue: Filtering Empty Strings

The problem arises in the second condition. Magento uses MySQL for its database operations, which treats empty strings literals as NULL. So when you try to filter for parent_customer = '', it’s actually filtering for NULL values.

However, there is no straightforward way to filter for non-NULL or non-empty values directly in the collection using addFieldToFilter.

The Solution: Filtering Non-Empty Values

To solve this issue, we need to modify our approach. Instead of trying to filter empty strings, let’s find a way to filter non-empty values.

We can achieve this by using the Zend_Db_Select object directly and manually building our query. This requires some additional steps:

  1. Get the collection as a Zend_Db_Select object.
  2. Add a condition for the field not being NULL or empty.
  3. Apply the modified select to the collection.

Here’s an example of how you can modify the _callbackParentCustomer method to achieve this:

protected function _callbackParentCustomer($collection, $column)
{
    if ($value === null) {
        return $this;
    }

    $select = $collection->getSelect();
    $fieldName = 'parent_customer';

    // For non-empty values (not NULL or not '')
    if ($value != 0) {
        Mage::log('show subaccounts', null, 'product.log', true);
        $select->where("IFNULL({$fieldName}, '') <> ''");
        // OR
        // $select->where("COALESCE({$fieldName}, '') <> ''");
    } else {
        Mage::log('hide subaccounts', null, 'product.log', true);
        $select->where("IFNULL({$fieldName}, '') = ''");
    }

    return $this;
}

In this modified version, we’re using the IFNULL function to handle NULL values and comparing it with an empty string. This effectively filters for non-empty values.

Conclusion

Filtering Magento grids can be complex but rewarding once you understand the underlying mechanisms. By directly manipulating the Zend_Db_Select object, we can overcome the limitations of standard filter callbacks.

Remember that working with custom fields and filters requires a good understanding of SQL queries and Magento’s core data handling processes.

For more advanced tips on customizing Magento grids, I recommend checking out the official documentation or visiting LaravelCompany for in-depth tutorials and best practices.

Catalog API Admin Customers

Our network