0

If we want to search by name or sku, we can just alter the their respective attribute settings in admin panel. But how to search by mentioning product id?

I thought changing Mage_Adminhtml_Model_Search_Catalog would does the same but I figured it out that it doesn't.

    $collection = Mage::helper('catalogsearch')->getQuery()->getSearchCollection()
        ->addAttributeToSelect('entity_id') /* added */
        ->addAttributeToSelect('name')
        ->addAttributeToSelect('description')
        ->addSearchFilter($this->getQuery())
        ->setCurPage($this->getStart())
        ->setPageSize($this->getLimit())
        ->load();

Please help.

  • Later, I realized searching by product id is not possible in magento. even though if we get a way to do it, we end up writing a big code.. better move to other attributes which are similar to product ids. – Mr_Green Apr 1 '14 at 6:58
2

You need to edit the Mage_CatalogSearch_Model_Resource_Search_Collection resource model.

Particularly the _getSearchEntityIdsSql function

I'm not yet sure how to add your code here, I'll search and update my post, except if one expert can help you replacing this resource model.

Edit. Apparently there is a foreach on each searchable attribute that append the results in a $selects[] array.

Try adding your request in the $selects array just after the end of foreach.

  • 1
    Sir, I am not good with sql, php and even magento.. if you just guide me with some code that will be better for me. I hope you have time for that :) – Mr_Green Dec 24 '13 at 5:39
0

Try this.

$product_collection = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('entity_id', array('eq' => $searchstring))
    ->load();
  • Su123, Where should I add this code? I tried in my above shown code, still it doesn't search by id... – Mr_Green Dec 23 '13 at 12:56
  • what is $searchstring here? – Mr_Green Dec 23 '13 at 13:06
  • Sorry. I couldn't mention. It is the product id which you enter for search – Sukeshini Dec 23 '13 at 15:13
0

you can try this to search by id:

$productsCollection = Mage::getModel('catalog/product')->getCollection()
                    ->addAttributeToFilter('entity_id', array('in' => $ids));
  • Sohil, where should I add this code? I tried in my above shown code, still it doesn't search by id... – Mr_Green Dec 23 '13 at 12:57

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.