Magento Expert Forum - Improve your Magento experience

Results 1 to 5 of 5

How to add search by category in Magento mini search?

  1. #1

  2. #2
    Junior Member Magento MageSolution's Avatar
    Join Date
    Dec 2013
    Posts
    203
    Thanks
    0
    Thanked 6 Times in 6 Posts

  3. #3
    Junior Member Bizmagestore's Avatar
    Join Date
    Apr 2014
    Location
    India
    Posts
    494
    Thanks
    1
    Thanked 27 Times in 27 Posts

    Default

    Give your customers a quicker and filtered search results. Help them search product catalogue faster with relevant attributes! Help them get an improved shopping experience! Retain and acquire more customers! http://store.biztechconsultancy.com/...-with-solr.htm

  4. #4
    Junior Member Gowebbaby's Avatar
    Join Date
    May 2014
    Location
    700 LOWER STATE RD, 19B3 NORTH WALES, PA 19454
    Posts
    42
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default

    Hi Thuong Tong,

    There is no free extension in magento commerce which will help you to search your store with particular category. I tried from myself and able to do, I am sharing my exprerience here with.
    Name:  megento.jpg
Views: 282
Size:  11.6 KB
    First create the drop down list of all category in form.mini.phtml

    <select name="category" id="category_search_field">
    <option value="">-- Any Category --</option>
    <?php foreach ($catalog->getStoreCategories() as $_category): ?>
    <?php if($_category->hasChildren()): ?>
    <option class="parent-cat" value="<?php echo $_category->getId(); ?>"><?php echo $_category->getName();?></option>
    <?php foreach ($_category->getChildren() as $subcategory):
    if($subcategory->getIsActive()) : ?>
    <option value="<?php echo $subcategory->getId(); ?>"<?php echo ($this->getRequest()->getQuery('category') == $subcategory->getId() ? ' selected="selected"': "") ?>><?php echo $subcategory->getName(); ?></option>
    <?php endif; endforeach; ?>
    <?php elseif($_category->getIsActive()): ?>
    <option value="<?php echo $_category->getId(); ?>"><?php echo $_category->getName();?></option>
    <?php endif; ?>
    <?php endforeach ?>
    </select>

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

    public function getStoreCategories()
    {
    $helper = Mage::helper('catalog/category');
    return $helper->getStoreCategories();
    }
    public function getSelectedCategory()
    {
    $catid = (int)addslashes($_REQUEST['category']);
    $cat="";
    if($catid>1)
    $cat = Mage::getModel('catalog/category')->load($catid);
    return $cat;
    }

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

    replace

    public function prepareProductCollection($collection)
    {
    $collection
    ->addAttributeToSelect(Mage::getSingleton('catalo g/config')->getProductAttributes())
    ->addSearchFilter(Mage::helper('catalogsearch')->getQuery()->getQueryText())
    ->setStore(Mage::app()->getStore())
    ->addMinimalPrice()
    ->addFinalPrice()
    ->addTaxPercents()
    ->addStoreFilter()
    ->addUrlRewrite();
    Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
    Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection) ;
    return $this;
    }

    with

    public function prepareProductCollection($collection)
    {
    if(Mage::helper('catalogsearch')->getSelectedCategory()!="")
    {
    $collection
    ->addAttributeToSelect(Mage::getSingleton('catalo g/config')->getProductAttributes())
    ->addSearchFilter(Mage::helper('catalogsearch')->getQuery()->getQueryText())
    ->setStore(Mage::app()->getStore())
    ->addMinimalPrice()
    ->addFinalPrice()
    ->addTaxPercents()
    ->addStoreFilter()
    ->addCategoryFilter(Mage::helper('catalogsearch')->getSelectedCategory())
    ->addUrlRewrite();
    }
    else
    {
    $collection
    ->addAttributeToSelect(Mage::getSingleton('catalo g/config')->getProductAttributes())
    ->addSearchFilter(Mage::helper('catalogsearch')->getQuery()->getQueryText())
    ->setStore(Mage::app()->getStore())
    ->addMinimalPrice()
    ->addFinalPrice()
    ->addTaxPercents()
    ->addStoreFilter()
    ->addUrlRewrite();
    }
    Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
    Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection) ;

    return $this;
    }

    I hope this is helpful for you.

    5 Features That Makes Magento An Outstanding ECommerce Platform

  5. #5
    Junior Member thaonvp's Avatar
    Join Date
    Mar 2015
    Posts
    42
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    If you want to do something like limit your search to items in top-level categories, you could add radio buttons to the form-mini to accomplish this.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •