Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Enhanced Product Grid Name Search [SOVLED]
 
elfling
Enthusiast
 
Avatar
Total Posts:  901
Joined:  2008-10-21
 

Hi All,

It’s been a while, but I’ve been really struggling to get this one out of the park.

Basically, I would like to enhance the product name filter on the product grid and what I have so far works, but not in the way that I would like it.

I’ve used some code from Inchoo to get me started, but I just keep coming across brick wall after brick wall when it comes down to narrowing the search down.

Basically, what needs to happen is as follows… You search on the product_catalog_grid in the name field the following

% harddrive% nlike, % 5400rpm%

This search would then return all the products with harddrive in the name, but exclude the 5400rpm drives.

I’ve updated the _addColumnToFilter function as follows, the main function actually coming from an Inchoo post.

protected function _addColumnFilterToCollection($column)
    
{
        
if ($this->getCollection()) {
            
if ($column->getId() == 'websites'{
                $this
->getCollection()->joinField('websites',
                    
'catalog/product_website',
                    
'website_id',
                    
'product_id=entity_id',
                    
null,
                    
'left');
            
}
            
if($column->getId() == 'name' && strpos($column->getFilter()->getValue(), ',')) {
                
            $_inNin 
explode(','$column->getFilter()->getValue());
            
$inNin = array();
            foreach (
$_inNin as $k => $v{
                
if (is_string($v) && strlen(trim($v))) {
                    $inNin[] 
trim($v);
                
}
            }
            
if (count($inNin)>&& in_array($inNin[0], array('in''nin''like''nlike'))) {
                $in 
$inNin[0];
                
$values array_slice($inNin1);
                
$this->getCollection()->addFieldToFilter($column->getId(), array($in => $values));
                return;
            

           
else {
               
return parent::_addColumnFilterToCollection($column);
            
}
            Zend_Debug
::dump((string)$this->getCollection()->getSelect(), 'Filter column step: ');
        
            
}
        }    
        
return parent::_addColumnFilterToCollection($column);
    
}

I’ve posted this code because it works, but only if I only want to search a single nin, in, nlike or like based search, which is obviously the case, because thats how it written to perform the task. Would anyone be able to help me expand on this, I think it could actually be pretty good for future releases of Magento to be honest.

It’s not often I ask for help on the forums, but for this one I’m unable to think straight as to how to achieve what I want. I believe 2 addFieldToFilter events will need to take place to perform the search operation that I require.

Thanks for your time, if anyone has any to answer my post that is.

Many Thanks,

Elfling

 
Magento Community Magento Community
Magento Community
Magento Community
 
elfling
Enthusiast
 
Avatar
Total Posts:  901
Joined:  2008-10-21
 

There must be a more efficient way of doing this, but so far I have.

if($column->getId() == 'name' && strpos($column->getFilter()->getValue(), ',')) {
            
            $pattern1 
"/(\w+\s){0}nlike,(\s\w+){1}/";
            
$pattern2 '/\[([^\]]*)\]/';

                if (
preg_match($pattern2$column->getFilter()->getValue(), $likeFilter) && preg_match($pattern1$column->getFilter()->getValue(), $nlikeFilter))    
                
{
                    
                    $like 
$likeFilter[1];
                    
$nlike str_replace("nlike,","",$nlikeFilter[0]);
                    
                    
$this->getCollection()->addFieldToFilter($column->getId(), array('like' => '%'.$like.'%'));
                    
$this->getCollection()->addFieldToFilter($column->getId(), array('nlike' => '%'.$nlike.'%'));
                     
Zend_Debug::dump((string)$this->getCollection()->getSelect(), 'Filter column step: ');
                    return;
                
}
            }
 
Magento Community Magento Community
Magento Community
Magento Community
 
elfling
Enthusiast
 
Avatar
Total Posts:  901
Joined:  2008-10-21
 

if($column->getId() == 'name' && strpos($column->getFilter()->getValue(), ']')) {
            
            $pattern1 
"/(\w+\s){0}\](\s\w+)*/";
            
$pattern2 '/\[([^\]]*)\]/';
            
                if (
preg_match($pattern2$column->getFilter()->getValue(), $nlikeFilter) && preg_match($pattern1$column->getFilter()->getValue(), $likeFilter))    
                
{

                    $like 
str_replace("] ","",$likeFilter[0]);
                        
                        if(
strpos($nlikeFilter[1]','))
                        
{
                            
                            $exploded 
explode(','$nlikeFilter[1]);
                            foreach (
$exploded as $nlike{
                            $this
->getCollection()->addFieldToFilter($column->getId(), array('nlike' => '%'.$nlike.'%'));
                                
                            
}
                        }
                        
else {
                            $nlike 
$nlikeFilter[1];
                            
$this->getCollection()->addFieldToFilter($column->getId(), array('nlike' => '%'.$nlike.'%'));
                        
}
    
                    
                    $this
->getCollection()->addFieldToFilter($column->getId(), array('like' => '%'.$like.'%'));
                    
                     
Zend_Debug::dump((string)$this->getCollection()->getSelect(), 'Filter column step: ');
                    return;
                
}
            }

Updated to run as search query

[excluded search terms] regular search

Example

[1tb, 500gb, 250gb] harddrive

This will filter the admin product grid for harddrives, but exclude 1tb, 500gb and 250gb from the filter.

 
Magento Community Magento Community
Magento Community
Magento Community
 
elfling
Enthusiast
 
Avatar
Total Posts:  901
Joined:  2008-10-21
 

Can someone delete the spam post please or delete the user account?

Would do it myself, but oh wait… no access smile

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top