A-A+

为magento的目录产品列表增加A-Z的产品首字母过滤功能

2011年07月12日 灵犀一指 评论 2 条 阅读 1,660 人 次

如何才能为magento的目录产品列表增加A-Z的产品首字母过滤功能,官方有一个插件,我把代码抽像出来供大家使用
效果图如下:

首先将
app/code/core/Mage/Catalog/Block/Product/List/Toolbar.php
拷到
app/code/local/Mage/Catalog/Block/Product/List/Toolbar.php
然后增加一个函数

public function str_replace_once($needle , $replace , $haystack){
    // Looks for the first occurence of $needle in $haystack
    // and replaces it with $replace.
		$pos = strpos($haystack, $needle);
		if ($pos === false) {
			// Nothing found
		return $haystack;
    	}
   return substr_replace($haystack, $replace, $pos, strlen($needle));
}

然后将以下函数换在如下代码

public function setCollection($collection)
    {
        $this->_collection = $collection;

        $this->_collection->setCurPage($this->getCurrentPage());

        // we need to set pagination only if passed value integer and more that 0
        $limit = (int)$this->getLimit();
		$postData = '';
        if ($limit) {
            $this->_collection->setPageSize($limit);
        }
        if ($this->getCurrentOrder())
		{
			/////Alphabate search Code Start From here.
			$postData = Mage::app()->getRequest()->getParam('alpha').'%';
			$postData_all = Mage::app()->getRequest()->getParam('alpha');
			if(isset($postData_all) && $postData_all!= '' && trim($postData_all) !='ALL')
			{

            $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection())->addAttributeToFilter(array(
                    array('attribute'=>'name', 'like'=>$postData)
                ));
			}else
			{

				   $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());

			}

			////Code End
        }
        return $this;
    }

在app/design/frontend/default/default/template/catalog/product/list/toolbar.phtml
尾部增加如下代码如可:

<div>

        <p>

            <?php
			$postData = Mage::app()->getRequest()->getParam('alpha');
			foreach ($search_array  as $search_array_value):
			if (strstr( $this->helper('core/url')->getCurrentUrl(), "?" ) )
			{
				$final_Url =  $this->str_replace_once('&','?',str_replace("?alpha=".trim($postData['alpha']),'',str_replace($make_nbsp."alpha=".trim($postData['alpha']),'',$this->helper('core/url')->getCurrentUrl())));

			}else
			{

				$final_Url = str_replace("?alpha=".trim($postData['alpha']),'',str_replace($make_nbsp."alpha=".trim($postData['alpha']),'',$this->helper('core/url')->getCurrentUrl()));
			}
			?>

            <a href="<?php echo $final_Url.$make_nbsp.'alpha='.$search_array_value;?>" title="<?php echo $_label ?>"><?php echo $search_array_value; ?></a>&nbsp;&nbsp;

            <?php endforeach; ?>

        </p>

    </div>

其实还是比较简单的,不好的地方就是要修改核心代码!

2 条留言  访客:1 条  博主:1 条

  1. 再见流星
    • 多乐士

评论已关闭!