I'm trying this app/code/Vendore/Sorting/etc/di.xml
<type name="Magento\Catalog\Model\Config">
<plugin name="config" type="Vendore\Sorting\Plugin\Catalog\Config" />
</type>
<type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
<plugin name="FilterInToolbar" type="Vendore\Sorting\Plugin\Catalog\Product\ProductList\Toolbar" />
</type>
app/code/Vendore/Sorting/Plugin/Catalog/Product/ProductList/Toolbar.php
public function aroundSetCollection(
\Magento\Catalog\Block\Product\ProductList\Toolbar $subject,
\Closure $proceed,
$collection
) {
$currentOrder = $subject->getCurrentOrder();
$result = $proceed($collection);
if ($currentOrder == 'mostviewed') {
$this->_collection->getSelect()
->joinLeft(
[
'report_event'
],
"(report_event.object_id = e.entity_id "
. "AND (report_event.event_type_id = 1))",
[
'view' => "count(report_event.event_id)"
]
)->group("e.entity_id")
->order('report_event.view ' . $this->getDirection($subject));
}
}
It's working in Magento 2.3 but not working in Magento 2.4 with Elasticsearch