最新消息:Excel无乱码转CSV,由于工作原因很少更新博客和回复大家的评论,非常抱歉。

[原创]给Popular Search Terms添加分页

在工作中,往往有很多事情等着你去完善,公司随着业务的扩大或对SEO的要求越来越高时,我们往往会把客户搜索过的一些关键词组织起来。

当词越来越多的时候,我们就想到了分页。就在这个时候,Popular Search Terms偏偏没有分页功能,怎么办?当然由技术来添加或二次开发了。

我就想,竟然Magento的功能这么强大,直接用它的内置的分页模块应该不能。谁知道,整了两天都没整出来,可能是我的技术还有彻底的了解Magento。可是时间不等人啊,怎么办?自己动写一个了,也许我这么做是多余的。

我也没办法,在网上求助也没有人给点意见,或提供一些方法以。去Magento官网论坛发过求助,在百度上发过提问,至今没有人回答我的问题,太杯具了。

好了,闲话少说下面就贴出我的开发步骤和代码:

该页面的地址:http://127.0.0.1/catalogsearch/term/popular
一、 建立一个Toolbar模板文件:
找到app\design\frontend\base\default\template\catalogsearch目录,在这里建一个名为toolbar.phtml的模板文件,在里面写入分页代码:

<?php if($this->getTotalNum()):?>
<div>
    <div>
        <p><?php echo $this->__('Items %s to %s of %s total', $this->getFirstNum(), $this->getLastNum(), $this->getTotalNum()) ?></p>
        <div>
            <strong><?php echo $this->__('Page:') ?></strong>
            <ol>
            <?php if (!$this->isFirstPage()): ?>
                <li>
                <a href="<?php echo $this->getPreviousPageUrl() ?>" title="<?php echo $this->__('Previous') ?>"><img src="<?php echo $this->getSkinUrl('images/pager_arrow_left.gif') ?>" alt="<?php echo $this->__('Previous') ?>" /></a>
                </li>
            <?php endif;?>

            <?php for ($_page=1;$_page <= $this->getFramePages();$_page++): ?>
                <?php if ($this->isPageCurrent($_page)): ?>
                    <li><?php echo $_page ?></li>
                <?php else: ?>
                    <li><a href="<?php echo $this->getPageUrl($_page) ?>"><?php echo $_page ?></a></li>
                <?php endif;?>
            <?php endfor;?>

            <?php if (!$this->isLastPage()): ?>
                <li>
                    <a href="<?php echo $this->getNextPageUrl() ?>" title="<?php echo $this->__('Next') ?>"><img src="<?php echo $this->getSkinUrl('images/pager_arrow_right.gif') ?>" alt="<?php echo $this->__('Next') ?>" /></a>
                </li>
            <?php endif;?>
            </ol>
        </div>
    </div>
</div>
<?php endif?>

这些代码是从page/html/pager.phtml里提取的。

二、 在Term.php类文件里一些方法:
找到app\code\core\Mage\CatalogSearch\Block目录下的Term.php文件,打开它在里面加入代码或重写,为了不破坏原来的可生写此类或备份此类。

<?php
class Mage_CatalogSearch_Block_Term extends Mage_Core_Block_Template
{
    protected $_terms;
 protected $_collection;
    protected $_minPopularity;
    protected $_maxPopularity;
 protected $_p = 1;
 protected $_limit = 5;

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Load terms and try to sort it by names
     *
     * @return Mage_CatalogSearch_Block_Term
     */
 public function getPageCount()
    {
  Mage::getSingleton('catalogsearch/session')->unsLimitPage();
  return (isset($_REQUEST['limit'])) ? intval($_REQUEST['limit']) : $this->_limit;
    }

 public function getCurPage()
 {
  if(isset($_REQUEST['p'])){
   if((int)$_REQUEST['p'] > $this->getFramePages()){
    $this->_p = $this->getFramePages();

   }elseif((int)$_REQUEST['p'] < 1){
    $this->_p = 1;

   }else{
    $this->_p = $_REQUEST['p'];
   }
  }

  return $this->_p; 
 }

    protected function _loadTerms()
    { 
        if (empty($this->_terms)) {
            $this->_terms = array();

            $terms = Mage::getResourceModel('catalogsearch/query_collection')
                ->setPopularQueryFilter(Mage::app()->getStore()->getId())
                ->setPageSize($this->getPageCount())
    ->setCurPage($this->getCurPage())
                ->load()
                ->getItems();

   /*$terms = $terms->setPageSize($this->getPageCount())
    ->setCurPage($this->getCurPage())
                ->load()
                ->getItems();*/

            if( count($terms) == 0 ) {
                return $this;
            }

            $this->_maxPopularity = reset($terms)->getPopularity();
            $this->_minPopularity = end($terms)->getPopularity();
            $range = $this->_maxPopularity - $this->_minPopularity;
            $range = ( $range == 0 ) ? 1 : $range;
            foreach ($terms as $term) {
                if( !$term->getPopularity() ) {
                    continue;
                }
                $term->setRatio(($term->getPopularity()-$this->_minPopularity)/$range);
                $temp[$term->getName()] = $term;
                $termKeys[] = $term->getName();
            }
            natcasesort($termKeys);

            foreach ($termKeys as $termKey) {
                $this->_terms[$termKey] = $temp[$termKey];
            }
        }
        return $this;
    }

    public function getCollection()
    {
        return $this->_loadTerms()->_terms;
    }

 public function getTotalNum(){

  $this->_collection = Mage::getResourceModel('catalogsearch/query_collection')
                   ->setPopularQueryFilter(Mage::app()->getStore()->getId());
  return count($this->_collection); 
 }

 public function getFramePages()
    {
  $n = $this->getTotalNum()/$this->_limit;

        return ceil($n);
    }

 public function getFirstNum()
    {
        return ($this->getCurPage()-1)+1;
    }

    public function getLastNum()
    {
        return $this->getPageCount()*($this->getCurPage()-1)+count($this->_loadTerms()->_terms);
    }

 public function getPreviousPageUrl()
    {
        return $this->getPageUrl($this->getCurPage()-1);
    }
 public function getNextPageUrl()
    {
        return $this->getPageUrl($this->getCurPage()+1);
    }

 public function isFirstPage()
    {
        return $this->getCurPage() <= 1;
    }

 public function isLastPage()
    {
        return $this->getCurPage() >= $this->getFramePages();
    }

 public function getPageUrl($page)
    {
        return '?p='.$page;
    }
 public function getPageVarName()
    {
        return $this->_pageVarName;
    }

 public function isPageCurrent($page)
    {
        return $page == $this->getCurPage();
    }

    public function getSearchUrl($obj)
    {
        $url = Mage::getModel('core/url');
        /*
        * url encoding will be done in Url.php http_build_query
        * so no need to explicitly called urlencode for the text
        */
        $url->setQueryParam('q', $obj->getName());
        return $url->getUrl('catalogsearch/result');
    }

    public function getMaxPopularity()
    {
        return $this->_maxPopularity;
    }

    public function getMinPopularity()
    {
        return $this->_minPopularity;
    }

}
?>

以上就是Term.php类的代码。

三、 以Catalogsearch.xml里加入Block:
找到app\design\frontend\base\default\layout目录下的catalogsearch.xml,在138行后加入:

<block type="catalogsearch/term" name="catalogsearch_toolbar" as="catalogsearch_toolbar" template="catalogsearch/toolbar.phtml" >
 <block type="catalogsearch/term" name="catalogsearch_term_toolbar" />
</block>

也就是加在catalogsearch/term.phtml模板Block之间,注意后面结束标签</block>。

四、 在模板term.phtml里添加Block:
找到app\design\frontend\base\default\template\catalogsearch目录下的term.phtml模板,在你要显示分页的地方加上:

<?php echo $this->getChildHtml('catalogsearch_toolbar') ?>

到目前为止,Popular Search Terms分页就完成了。

如果觉得我的内容不好,你可以在下面喷我。如果你更好的解决方案也可以在下方提出,谢谢!

代码下载:Popular-Search-Terms-app.zip

转载请注明:嗨酷哥,有你更酷! » [原创]给Popular Search Terms添加分页

与本文相关文章

发表我的评论

取消评论
表情 插代码

Hi,您需要填写昵称和邮箱!

  • 必填项
  • 必填项

网友评论1条  本站回复1

  1. 😮 路过

    天意2011-12-09 09:18 回复