-->

How to add Rating to sort list in Magento 1.7

2019-07-21 02:24发布

问题:

Looking for some help adding sort by Rating in Magento. I have added code snippets to toolbar.php which seem to add the sort by Rating but when trying to select it, it gets stuck until I reload the page. Any help would be greatly appreciated. Code can be found below: This is the Toolbar.php file.

// Begin new Code

    $this->getCollection()->joinField('rating',
        'review/review_aggregate',
        'rating_summary',
        'entity_pk_value=entity_id',
        '{{table}}.store_id=1',
        'left');
    // End new Code 

AND

// Add rating to "Sort by"

$_availableOrder = $this->_availableOrder;
$_availableOrder['rating'] = 'Rating';

return $_availableOrder;

$this->_availableOrder = array(
‘rating_summary’ => Mage::helper(’catalog’)->__(’Rating’),
‘price’ => Mage::helper(’catalog’)->__(’Price’),
‘newest’ => Mage::helper(’catalog’)->__(’Newest’),
‘name’ => Mage::helper(’catalog’)->__(’Name’)        
);

回答1:

Best is to make this in a module but here you go:

First we shall alter the way products are retrieved from the database, to include the overall rating (shown as the number of stars on the product) along with the rest of the product attributes. Copy the file app/code/core/Mage/Catalog/Block/Product/List.php to app/code/local/Mage/Catalog/Block/Product/List.php and open it for editing.

In the new List.php file find the following line (around line 86):

$this->_productCollection = $layer->getProductCollection();

After this add the following:

$this->_productCollection->joinField('rating_summary', 'review_entity_summary', 'rating_summary', 'entity_pk_value=entity_id', array('entity_type'=>1, 'store_id'=> Mage::app()->getStore()->getId()), 'left');

Now we need to add in an option so that the customer can select "Rating" as an attribute to sort by. Copy the file app/code/core/Mage/Catalog/Model/Config.php to app/code/local/Mage/Catalog/Model/Config.php and edit.

In the new Config.php file find the following code (which should start around line 298):

$options = array(
    'position'  => Mage::helper('catalog')->__('Position')
);

Replace with code with:

$options = array(
    'position'  => Mage::helper('catalog')->__('Position'),
    'rating_summary' => Mage::helper('catalog')->__('Rating')
);

Now when viewing categories on your website you should have an option of "Rating" in addition to the others. Note that the sort order defaults to ascending so the lowest rated products will be displayed first. The sort order can be changed by the customer by clicking the arrow next to the drop-down box. Aside from this caveat the new sort is fairly easy to implement and extends the usefulness of the ratings.

Credits: https://www.fontis.com.au/blog/sort-products-rating