8

I have a requirement where if any integer is there in Magento quick search string, then do a search looking for product SKU and custom attributes. But if there are no integers in the search string, then search looking in product Name and Description.

Let's say the query string has 12345 in it, then I would want to do a LIKE search in the product attributes SKU and few other custom attributes. But if there is no integer in the query string, then I would want to do a FULLTEXT search in the product Name and Description only. Makes sense?

I have looked into CatalogSearch/Model/Resource/Fulltext.php -> prepareResult() but can't figure out how to filter results based on query string. Also, the quick search is using catalogsearch/result model to search, whereas here we may need to look into catalog/product?

I am lost, any help would be greatly appreciated!

3

Try creating a new module

In your config.xml

...
<adminhtml>
    <global_search>
        <magepal_custom_attributes>
            <class>MagePal_GlobalAttributesSearch_Model_Search_Customattributes</class>
            <acl>magepal_globalattributessearch</acl>
        </magepal_custom_attributes>
    </global_search>
</adminhtml> 
    ...

in app/code/local/MagePal/GlobalAttributesSearch/Model/Search/Customattributes.php

<?php
class MagePal_GlobalAttributesSearch_Model_Search_Customattributes extends Varien_Object
{
    /**
     * Load search results
     *
     */
    public function load() {
        $arr = array();
        $searchText = $this->getQuery();


        // move code above to your if statement and do your db lookup accordingly 
        $collection = Mage::getModel('module/name')->getCollection()

         if(is_int($searchText)){
           /* search for int in product sku and custom */
            $collection->addFieldToFilter(
                array('field_name'),
                array(
                    array('like'=>'%'.$searchText.'%'), 
                    )
                );
         }
         else{
            /* search product name and description  */
            $collection->addFieldToFilter(
                array('field_name','field_name'),
                array(
                    array('like'=>'%'.$searchText.'%'),
                    array('like'=>'%'.$searchText.'%'), 
                    )
                );
         }

        $collection->load();

        foreach ($collection as $model) {
            $arr[] = array(
                'id'            => 'path/1/'.$model->getId(),
                'type'          => Mage::helper('adminhtml')->__('Custom Attributes'),
                'name'          => $model->getId(),
                'description'   => Mage::helper('core/string')->truncate('desc', 35),
                'url' => Mage::helper('adminhtml')->getUrl('*/path/edit', array('id'=>$model->getId())),
            );
        }

        $this->setResults($arr);
        return $this;
    }
}

See http://blog.mattstephens.co.uk/post/27326981315/adding-custom-module-to-magentos-admin-global-search

You could also rewrite the global product search block.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.