I have an issue from last three days.I make a sorting option by Best Seller on Category listing Page.
Here You See three Products but item shows 1 which occurs issue on paging.
I used aroundSetCollection for Best Seller Sorting.Here This:
public function aroundSetCollection(
\Magento\Catalog\Block\Product\ProductList\Toolbar $subject,
\Closure $proceed,
$collection
) {
$currentOrder = $subject->getCurrentOrder();
$result = $proceed($collection);
$collection = $subject->getCollection();
if ($currentOrder == 'best_selling') {
$year = date("Y-m-d H:i:s", strtotime('-1 year'));
$now = date("Y-m-d H:i:s");
$collection->getSelect()->joinInner(
'sales_order_item',
'e.entity_id = sales_order_item.product_id',
array('total_orders' => 'SUM(sales_order_item.qty_ordered)')
)->where(
'sales_order_item.created_at >= ?',
$year
)->where(
'sales_order_item.created_at <= ?',
$now
)->group('e.entity_id')
->order('total_orders '.$subject->getCurrentDirection());
}
return $result;
}
And di.xml :
<type name="Magento\Catalog\Model\Config">
<plugin name="add_bestselling_option" type="NameSpace\ModuleName\Plugin\Model\Config" />
</type>
<type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
<plugin name="set_bestselling_collection" type="NameSpace\ModuleName\Plugin\Product\ProductList\Toolbar" />
</type>
Please help me i am stuck here from last three days.