1

My Store has some Categories and sub-categories as :
-- Default Category
     -- Pass-over
         -- EP5
         -- EP15
     -- Self-serve
         -- EP02
         -- EP99
In my mini search form, It Shows Pass-over, Self-serve in Category search

I want EP5, EP15 ni category search,
My form.mini.phtml code for mini search is :

<div class="mid_part_bg_img">
    <form id="search_mini_form" action="<?php echo $catalogSearchHelper->getResultUrl() ?>" method="get">

        <div class="form-search">
        <?php
    $category = Mage::getModel('catalog/category');
    if(is_object(Mage::registry('current_category'))){
        $currentCategoryPath=Mage::registry('current_category')->getPathIds();
    }else{
        $currentCategoryPath = array();
    }
    $category->load(Mage::app()->getStore()->getRootCategoryId());
    $childrenString = $category->getChildren();
    $children = explode(',',$childrenString);
    $categoryOptions='';
    foreach($children as $c){
        if($this->getRequest()->getParam('cat')==""){
            $selected = (in_array($c, $currentCategoryPath))?'SELECTED':'';
        }else{
            $selected = ($this->getRequest()->getParam('cat')==$c)?'SELECTED':'';
    }
    $categoryOptions.= '<option value="' . $c . '" ' . $selected . '>' . $category->load($c)->getName() . '</option>' . "\n";
    }
?>

           <select name="cat" id="cat">
    <option value="">All</option>
    <?php echo $categoryOptions ?>
</select>
            <input id="search" type="text" name="<?php echo $catalogSearchHelper->getQueryParamName() ?>" value="<?php echo $catalogSearchHelper->getEscapedQueryText() ?>" class="input-text"  maxlength="<?php echo $catalogSearchHelper->getMaxQueryLength();?>" />

            <button type="submit" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Search')) ?>" class="button search_btn"><i class="icon-search"></i></button>
            <div id="search_autocomplete" class="search-autocomplete"></div>
            <div class="clearfix"></div>
            <script type="text/javascript">
            //<![CDATA[
                var searchForm = new Varien.searchForm('search_mini_form', 'search', '<?php echo Mage::helper('core')->jsQuoteEscape($this->__('Search entire store here...')) ?>');
                searchForm.initAutocomplete('<?php echo $catalogSearchHelper->getSuggestUrl() ?>', 'search_autocomplete');
            //]]>
            </script>
        </div>
    </form>
</div>
3

add below function in your helper

function get_categories()
{
    $category = Mage::getModel('catalog/category'); 
    $treeModel = $category->getTreeModel(); 
    $treeModel->load();

    $ids = $treeModel->getCollection()->getAllIds(); 

    $data = array();

    if (!empty($ids))
    { 
        foreach ($ids as $id)
        { 
            $cat = Mage::getModel('catalog/category'); 
            $cat->load($id); 
            $categoryData = array('id'=>$cat->getId(),
                              'url'=>$cat->getUrl(),
                              'name'=>$cat->getName(),
                              'image'=>$cat->getImageUrl(),
                              'isActive'=>$cat->getIsActive()
                             );
            if($id>2)
            {
                array_push($data, $categoryData);
            } 
        } 
    }

    return $data;
}

in form.mini.phtml include below code into form

<select id="category" class="search_select" name="cat">
     <option value="">category</option>
     <?php 
     $categories = Mage::helper('<your_module>')->get_categories();
     #echo '<pre/>';print_r($categories);exit;
     foreach($categories as $k=>$v) 
     {
     ?>
     <option value="<?php echo $v['id']; ?>" <?php echo  (isset($_GET['cat']) && isset($_GET['q']) && $_GET['cat'] == $v['id'] )? 'selected':''; ?>><?php echo $v['name']; ?></option>
     <?php 
     } 
     ?>
 </select>
  • its not working – Himani.VC Apr 6 '17 at 10:45
  • instead of not working will you say what exactly you are getting. First make sure you are getting array return fron your module @Himani.VC – Gopal Patel Apr 6 '17 at 12:52
  • working great :) – Shiv Singh Jul 12 '17 at 8:02

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.