最模板 - 外贸网站建设,外贸网站模板

最模板

当前位置: 首页 > Magento > Magento教程 >

magento 搜索自动完成AutoComplete

时间:2014-07-11 00:52来源:未知 作者:最模板zuimoban 点击:
magento 搜索自动完成 一:base\default\template\catalogsearch form.mini.phtml div id=search_autocomplete class=search-autocomplete/div script type=text/javascript //![CDATA[ var searchForm = new Varien.searchForm(search_mini_form, se

magento 搜索自动完成

autocomplete

autocomplete

 

一:base\default\template\catalogsearch  form.mini.phtml
 
<div id=”search_autocomplete” class=”search-autocomplete”></div>
<script type=”text/javascript”>
//<![CDATA[
var searchForm = new Varien.searchForm('search_mini_form', 'search', '<?php echo $this->__('Search entire store here...') ?>');
searchForm.initAutocomplete('<?php echo $catalogSearchHelper->getSuggestUrl() ?>', 'search_autocomplete');
//]]>
</script>
 
二:提交到croller  app\code\core\Mage\CatalogSearch\controllers   AjaxController.php
 
 
/**
* Catalog Search Controller
*
* @category Mage
* @package Mage_CatalogSearch
* @module Catalog
*/
class Mage_CatalogSearch_AjaxController extends Mage_Core_Controller_Front_Action
{
public function suggestAction()
{
if (!$this->getRequest()->getParam('q', false)) {
$this->getResponse()->setRedirect(Mage::getSingleton('core/url')->getBaseUrl());
}
 
$this->getResponse()->setBody($this->getLayout()->createBlock('catalogsearch/autocomplete')->toHtml());
}
}
 
 
 
 
三: block 数据处理返回 app\code\core\Mage\CatalogSearch\Block Autocomplete.php
 
 
/**
* Autocomplete queries list
*/
class Mage_CatalogSearch_Block_Autocomplete extends Mage_Core_Block_Abstract
{
protected $_suggestData = null;
 
protected function _toHtml()
{
$query = $this->helper('catalogsearch')->getQueryText();
 
$html = '';
 
if (!$this->_beforeToHtml()) {
return $html;
}
 
$suggestData = $this->getSuggestData();
if (!($count = count($suggestData))) {
return $html;
}
 
$count--;
 
$html = '<ul><li style="display:none"></li>';
foreach ($suggestData as $index => $item) {
if ($index == 0) {
$item['row_class'] .= ' first';
}
 
if ($index == $count) {
$item['row_class'] .= ' last';
}
 
$html .= '<li title="'.$this->htmlEscape($item['title']).'" class="'.$item['row_class'].'"><a href="'.$item['link'].'"><span class="title">'
.$item['title'] . '</span><span class="amount">'.$item['num_of_results'].'</span></a></li>';
}
 
$html.= '</ul>';
 
return $html;
}
 
public function getSuggestData()
{
if (!$this->_suggestData) {
$productTable = Mage::getSingleton('core/resource')->getTableName('catalog/category_product');
$query = $this->helper('catalogsearch')->getQueryText();
$collection = Mage::getResourceModel('catalog/category_collection')
->setStore(Mage::app()->getStore())
->addAttributeToSelect('name')
->addAttributeToSelect('url_key')
->addFieldToFilter('is_active', 1);
 
$counter = 0;
$data = array();
foreach ($collection as $item) {
$_data = array(
'title' => $this->getCategoryName($item),
'row_class' => (++$counter)%2?'odd':'even',
'link' => $item->getUrl(),
'num_of_results' => $this->__('%d products',$item->getProductCount())
);
 
if ($item->getName() == $query) {
array_unshift($data, $_data);
}
else {
$data[] = $_data;
}
if ($counter > 8) break;
}
$this->_suggestData = $data;
}
return $this->_suggestData;
}
 
public function getCategoryName($category)
{
$name = '';
 
foreach(array_reverse($category->getParentCategories()) as $item){
if($item->getLevel() == 1) break;
$name .= $item->getName().' ';
}
 
return $name;
}
}
(责任编辑:最模板)
------分隔线----------------------------
栏目列表
推荐内容