diff --git a/app/code/Magento/Catalog/Block/Product/ProductList/Toolbar.php b/app/code/Magento/Catalog/Block/Product/ProductList/Toolbar.php index e07385262e9b838bd4433f195444cbffc623afb1..f1567c59b600469a8ddafca1c6fa64427a731a76 100644 --- a/app/code/Magento/Catalog/Block/Product/ProductList/Toolbar.php +++ b/app/code/Magento/Catalog/Block/Product/ProductList/Toolbar.php @@ -198,36 +198,21 @@ class Toolbar extends \Magento\Framework\View\Element\Template $this->getCurrentDirection() )->addAttributeToSort('entity_id', $this->getCurrentDirection()); }else if($this->getCurrentOrder() == 'rate') { - if ( $this->getCurrentDirection() == 'desc' ) { - $this->_collection - ->getSelect() - ->order('e.created_at DESC'); - - - } elseif ( $this->getCurrentDirection() == 'asc' ) { - - $this->_collection - ->getSelect() - ->order('e.created_at ASC'); - - } + $this->_collection->getSelect()->joinLeft( + 'rating_option_vote_aggregated', + 'e.entity_id = rating_option_vote_aggregated.entity_pk_value', + array('vote_count' => 'SUM(rating_option_vote_aggregated.vote_count)')) + ->group('e.entity_id') + ->order('vote_count desc'); }else if($this->getCurrentOrder() == 'popular') { - if ( $this->getCurrentDirection() == 'desc' ) { - - $this->_collection - ->getSelect() - ->order('e.created_at DESC'); - - - } elseif ( $this->getCurrentDirection() == 'asc' ) { - - $this->_collection - ->getSelect() - ->order('e.created_at ASC'); - - } + $this->_collection->getSelect()->joinLeft( + 'sales_order_item', + 'e.entity_id = sales_order_item.product_id', + array('qty_ordered'=>'SUM(sales_order_item.qty_ordered)')) + ->group('e.entity_id') + ->order('qty_ordered desc'); }else{ $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());