选择站点语言

简体中文English
首页 关于我们 公司博客 Magento: 如何对产品进行分目录的产品搜索 (摘自公司英文博客)

Magento: 如何对产品进行分目录的产品搜索 (摘自公司英文博客)

14
12月
2009

In "advanced search" feature, searching product by category is not the default setting.  But we can do this by modifying the foloowing files:

app/code/core/Mage/CatalogSearch/Block/Advanced/Form.php
app/code/core/Mage/CatalogSearch/Model/Advanced.php
app/design/yourdesign/yourdesign/template/catalogsearch/advanced/form.phtml

 

At the bottom of app/code/core/Mage/CatalogSearch/Block/Advanced/Form.php we can add following codes:


public function getStoreCategories()
    {
        $helper = Mage::helper('catalog/category');
        return $helper->getStoreCategories();
    }

 

In app/code/core/Mage/CatalogSearch/Model/Advanced.php, replace the getSearchCriterias() by following functions:

 


 public function getSearchCriterias()
 
     {
     $search = $this->_searchCriterias;
              /* display category filtering criteria */
         if(isset($_GET['category']) && is_numeric($_GET['category'])) {
         $category = Mage::getModel('catalog/category')->load($_GET['category']);
         $search[] = array('name'=>'Category','value'=>$category->getName());
      }
      return $search;
     }

 

Replace the getProductCollection() by following function:


 public function getProductCollection(){
    if (is_null($this->_productCollection)) {
        $this->_productCollection = Mage::getResourceModel('catalogsearch/advanced_collection')
        ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
        ->addMinimalPrice()
        ->addStoreFilter();
        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($this->_productCollection);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($this->_productCollection);
        /* include category filtering */
        if(isset($_GET['category']) && is_numeric($_GET['category'])) $this->_productCollection->addCategoryFilter(Mage::getModel('catalog/category')->load($_GET['category']),true);
        }
        return $this->_productCollection;
} 


 

In page app/design/yourdesign/yourdesign/template/catalogsearch/advanced/form.phtml, find the codes as following:

 


   <input name="<?php echo $_code ?>" id="<?php echo $_code ?>" value="<?php echo $this->htmlEscape($this->getAttributeValue($_attribute)) ?>" title="<?php echo $this->htmlEscape($this->getAttributeLabel($_attribute)) ?>"  class="input-text <?php echo $this->getAttributeValidationClass($_attribute) ?>" type="text" />
            <?php endswitch; ?>
        </li>
        <?php endforeach; ?>

 

at the bottom of this piece of codes, add following codes:

 


         <li>
<label for="category_search_field">Search by Category:</label>
<select name="category" id="category_search_field">
<option value="">-- Any Category --</option>
<?php foreach ($this->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>
</li>

 

伊锐数码 版权所有 谢绝转载
分享到:
关于我们 最新案例 技术优势 公司博客

avatar

武汉伊锐数码技术开发有限公司是一家位于中国武汉的专业 PHP (Professional PHP) 软件外包企业 (CMMI3 认证中)。我们专注于世界最前沿的网络门户,内容管理,电子商务解决方案;具有多媒体及图形应用,搜索引擎优化等国际化经验。

公司在Zend Framework, Joomla!, Magento, TYPO3, Drupal, WordPress, Facebook, Android, 云计算, 物联网等项目上拥有国内顶尖的开发团队,为欧盟,美国,加拿大,港台及中国大陆本土客户提供开发外包,业务流程外包等服务,拥有大量国内外成功案例。

我们专业化的团队将确保您能获得世界范围内最佳的性价比方案,愿与您创建合作共赢的网络天地!

Email: info[AT]evebit.com  电话: +86-27-82701147  在线问询: 请点击 此处...

CMS 系统专家

做为CMS(内容管理系统)的解决方案提供商,我们是国内唯一同时精通 Joomla!, WordPress, Drupal, TYPO3 等系统,并交付商业项目给国内外客户的公司。小到政治人物, 著名歌星的Fans社区, 大到新闻门户, 交易平台; 我们都可以量体裁衣提供相应的CMS解决方案!

Magento 专家

我们拥有世界顶级的基于开源项目Magento的电子商务平台的建站经验, 能够快速高效的为您搭建一个安全的电子商务平台, 国际大师的设计水准和高端的用户体验让您的品牌形象迅速提升! (http://www.magentocommerce.com)

Zend Framework 框架

Zend Framework 具有高度的模块化和灵活性, 我们有着强大的 Zend 开发团队以及非常丰富的 Zend Framework 开发经验, 可以快速搭建基于 Zend Framework 的大中型电子政务和电子商务平台. (http://framework.zend.com)

Zend 官方权威认证

公司积聚了国内一大批顶级的PHP专业人才: 我们部分工程师通过了 Zend认证工程师 (ZCE) 的考试, 并且全部工程师都进行了基于 Zend 的权威技术认证训练; 保证研发团队都拥有非常扎实的PHP研发经验, 数据库优化, 系统调试等各方面的综合素质.

鄂ICP备000358号