tags:

views:

236

answers:

2

How can I add a database field to the Magento global search? I want to create a custom "business name" field in customers and then have it searchable with that global search bar on the administrative panel.

A: 

After some searching, I found it in Mage_Adminhtml_Model_Search_Customer. This class has a method load() that you can modify to return additional search results and changed subtext. Oddly enough, Magento actually has you load all possible search resutls for a search and then limits them, but regardless here's the code to add search on a custom field:

Old Code:

    $collection = Mage::getResourceModel('customer/customer_collection')
        ->addNameToSelect()
        ->joinAttribute('company', 'customer_address/company', 'default_billing', null, 'left')
        ->addAttributeToFilter(array(
            array('attribute'=>'firstname', 'like'=>$this->getQuery().'%'),
            array('attribute'=>'lastname', 'like'=>$this->getQuery().'%'),
            array('attribute'=>'company', 'like'=>$this->getQuery().'%'),
        ))
        ->setPage(1, 10)
        ->load();

I created a class in the local space to override this function and added the following:

    $collection = Mage::getResourceModel('customer/customer_collection')
        ->addNameToSelect()
        ->joinAttribute('company', 'customer_address/company', 'default_billing', null, 'left')
        ->joinAttribute('company_name', 'customer/company_name', 'entity_id', null, 'left')
        ->addAttributeToFilter(array(
            array('attribute'=>'firstname', 'like'=>$this->getQuery().'%'),
            array('attribute'=>'lastname', 'like'=>$this->getQuery().'%'),
            array('attribute'=>'company', 'like'=>$this->getQuery().'%'),
            array('attribute'=>'company_name', 'like'=>$this->getQuery().'%'),
        ))
        ->setPage(1, 10)
        ->load();

Now I can use the global search for my custom attribute! Hopefully that will help someone else.

Thanks, Joe

Joseph Mastey
A: 

"I created a class in the local space to override this function"

How did you create the local space, I am new to magento so dont know how to create a local space, would you please tell me steps to creae local space for this specific problem

Regards

Tofeeq

Tofeeq