1

I am working on Magento2.2.1, I want to sort by rating. I have applied the below code but it's not working with Ajax neither Page load as well. Pagination is not coming.

$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 ' . $this->getCurrentDirectionReverse());

Can anyone please suggest

1 Answer 1

1

For pagination issue make plugin in your custom module like

create di.xml file under etc directory of your module

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
 <type name="Magento\Catalog\Model\ResourceModel\Product\Collection">
        <plugin name="product-collection-sorting" type="NameSpace\ModuleName\Plugin\Product\Collection" sortOrder="1"/>
    </type>
</config>

create plugin file

<?php namespace NameSpace\ModuleName\Plugin\Product;

class Collection

{

public function afterGetSelectCountSql(\Magento\Catalog\Model\ResourceModel\Product\Collection $subject, $result)
{

   if(count($result->getPart(\Magento\Framework\DB\Select::GROUP)) > 0) {
       $result->reset(\Magento\Framework\DB\Select::GROUP);
   }
   return $result;
 }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.