Monday , December 11 2017
Home >> All tutorial >> How to add search by category in magento mini search

How to add search by category in magento mini search

There is no free extension in magento commerce which will help you to search your store with particular category. But you can do as following:

First create the drop down list of all category in form.mini.phtml

  1. <select name=“category” id=“category_search_field”>
  2. <option value=“”>– Any Category –</option>
  3. <?php foreach ($catalog->getStoreCategories() as $_category): ?>
  4. <?php if($_category->hasChildren()): ?>
  5. <option class=“parent-cat” value=“<?php echo $_category->getId(); ?>”><?php echo $_category->getName();?></option>
  6. <?php foreach ($_category->getChildren() as $subcategory):
  7. if($subcategory->getIsActive()) : ?>
  8. <option value=“<?php echo $subcategory->getId(); ?>”<?php echo ($this->getRequest()->getQuery(‘category’) == $subcategory->getId() ? ‘ selected=”selected”‘: “”) ?>><?php echo $subcategory->getName(); ?></option>
  9. <?php endif; endforeach; ?>
  10. <?php elseif($_category->getIsActive()): ?>
  11. <option value=“<?php echo $_category->getId(); ?>”><?php echo $_category->getName();?></option>
  12. <?php endif; ?>
  13. <?php endforeach ?>
  14. </select>

Now go to app/code/core/Mage/CatalogSearch/Helper and open the Data.php and add the below code:

  1. public function getStoreCategories()
  2. {
  3. $helper = Mage::helper(‘catalog/category’);
  4. return $helper->getStoreCategories();
  5. }
  6. public function getSelectedCategory()
  7. {
  8. $catid = (int)addslashes($_REQUEST[‘category’]); 
  9. $cat=“”;
  10. if($catid>1)
  11. $cat = Mage::getModel(‘catalog/category’)->load($catid); 
  12. return $cat; 
  13. }

Now go to app/code/core/Mage/CatalogSearch/Model and open the Layer.php

replace

  1. public function prepareProductCollection($collection)
  2. {
  3. $collection
  4. ->addAttributeToSelect(Mage::getSingleton(‘catalog/config’)->getProductAttributes())
  5. ->addSearchFilter(Mage::helper(‘catalogsearch’)->getQuery()->getQueryText())
  6. ->setStore(Mage::app()->getStore())
  7. ->addMinimalPrice()
  8. ->addFinalPrice()
  9. ->addTaxPercents()
  10. ->addStoreFilter()
  11. ->addUrlRewrite();
  12. Mage::getSingleton(‘catalog/product_status’)->addVisibleFilterToCollection($collection);
  13. Mage::getSingleton(‘catalog/product_visibility’)->addVisibleInSearchFilterToCollection($collection);
  14. return $this;
  15. }
with
  1. public function prepareProductCollection($collection)
  2. {
  3. if(Mage::helper(‘catalogsearch’)->getSelectedCategory()!=“”)
  4. {
  5. $collection
  6. ->addAttributeToSelect(Mage::getSingleton(‘catalog/config’)->getProductAttributes())
  7. ->addSearchFilter(Mage::helper(‘catalogsearch’)->getQuery()->getQueryText())
  8. ->setStore(Mage::app()->getStore())
  9. ->addMinimalPrice()
  10. ->addFinalPrice()
  11. ->addTaxPercents()
  12. ->addStoreFilter()
  13. ->addCategoryFilter(Mage::helper(‘catalogsearch’)->getSelectedCategory())
  14. ->addUrlRewrite();
  15. }
  16. else
  17. {
  18. $collection
  19. ->addAttributeToSelect(Mage::getSingleton(‘catalog/config’)->getProductAttributes())
  20. ->addSearchFilter(Mage::helper(‘catalogsearch’)->getQuery()->getQueryText())
  21. ->setStore(Mage::app()->getStore())
  22. ->addMinimalPrice()
  23. ->addFinalPrice()
  24. ->addTaxPercents()
  25. ->addStoreFilter() 
  26. ->addUrlRewrite();
  27. } 
  28. Mage::getSingleton(‘catalog/product_status’)->addVisibleFilterToCollection($collection);
  29. Mage::getSingleton(‘catalog/product_visibility’)->addVisibleInSearchFilterToCollection($collection);

return $this;
}

save and Enjoy.

We also contribute great content of Magento Download for FREE (all versions)

DON'T MISS THE CHANCE!

magento download

About Champ

One comment

  1. Maha farzi code hai…..

Leave a Reply

Your email address will not be published. Required fields are marked *

Heap | Mobile and Web Analytics