简体   繁体   English

如何按前端受欢迎程度对Magento产品进行分类?

[英]How to sort Magento products by popularity in frontend?

I have created a module to add a new option in 'sort by' dropdown box in the category page. 我创建了一个模块,可以在类别页面的“排序依据”下拉框中添加新选项。 the new option should show in each and every category page same as 'position' option which is already there. 新选项应在每个类别页面中显示,与已经存在的“位置”选项相同。 I want to add 'popularity' as an option. 我想添加“人气”作为选项。 My Model class is executing and it's adding the option to the option array. 我的Model类正在执行,并且正在将选项添加到选项数组中。 But the block is not working. 但是该块不起作用。 Please I need your help to find the issue in my code? 请在您的代码中找到您需要的帮助吗?

Here is the Block class 这是Block类

<?php
class Tal_Popularity_Block_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar
{
    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();
       if ($limit) {
           $this->_collection->setPageSize($limit);
       }
        if($this->getCurrentOrder() == 'popularity'){
           $this->_collection->sortByReview($this->getCurrentDirection());
       }
     else if ($this->getCurrentOrder()) {

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

       return $this;
   }
}

following is my config.xml 以下是我的config.xml

<?xml version="1.0" ?>
 <config>
    <modules>
        <Tal_Popularity>
            <version>0.1.0</version>
        </Tal_Popularity>
    </modules>
    <frontend>
        <routers>
            <popularity>
                <use>standard</use>
                <args>
                    <module>Tal_Popularity</module>
                    <frontName>popularity</frontName>
                </args>
            </popularity>
        </routers>
    </frontend>
    <global>
    <models>
        <catalog>
            <rewrite>
                <config>Tal_Popularity_Model_Config</config>
            </rewrite>
        </catalog>
    </models>
     <blocks>
        <catalog>
            <rewrite>
                <catalog>Tal_Popularity_Block</catalog>
            </rewrite>
           </catalog>
        </blocks>
         <resources>
            <popularity_setup>
                <setup>
                    <module>Tal_Popularity</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </popularity_setup>
            <popularity_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </popularity_write>
            <popularity_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </popularity_read>
        </resources>
    </global>
</config>

I found the solution. 我找到了解决方案。 The issue was in the config.xml 问题出在config.xml中

Previous code. 先前的代码。

<blocks>
        <catalog>
            <rewrite>
                <catalog>Tal_Popularity_Block</catalog>
            </rewrite>
           </catalog>
        </blocks>

The above code should be like following. 上面的代码应如下所示。

 <blocks>
            <catalog>
                <rewrite>            <product_list_toolbar>Tal_Popularity_Block_Product_List_Toolbar</product_list_toolbar>
                </rewrite>
               </catalog>
            </blocks>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM