Magento – Modify catalogsearch module to search by attribute values

attributescatalogsearchcontrollerssearch

I have attempted for quite a number of hours to solve this but I can't find the solution.

I want to allow users to search by certain attribute values, for example ingredients, brand OR product name. At the moment I can pass to the query string the type of search we are performing, e.g.

/catalogsearch/result/?q=potato&type=ingredients

I have tried to modify the controllers and helpers in catalogsearch so I can pass this variable on and search for that filter, but I just can't figure out a way to do it.

I would still like to search by name too, so there will need to be a switch e.g.

/catalogsearch/result/?q=potato&type=name

Would need to search product name still.

Any tips for accomplishing this? I've been editing

/app/core/Mage/CatalogSearch/ 

for hours without any real progress.

Thanks 🙂

Best Answer

You can use this code to search product with many fields Below code is able to search by product name, description, using mysql LIKE syntax

//$data = search box input text

$products = Mage::getResourceModel('catalog/product_collection')
            ->addStoreFilter($storeId)
            ->addAttributeToSelect('*')
            ->setOrder('created_at', 'DESC')
            ->addAttributeToFilter(
                array(
                    array(
                        'attribute' => 'name',
                        'like'        => '%'. $data->getQueryText().'%',
                        )
                    ,
                    array(
                        'attribute' => 'description',
                        'like'        => '%'. $data->getQueryText().'%',
                        )

                    )
            )
            ->addExpressionAttributeToSelect('query_text', '{{name}}', 'name')
            ->addAttributeToFilter('visibility', $visibility)
            ->addAttributeToFilter(
                'status',
                array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
            );
Related Topic