Skip to content

Can't count right items when sort by bestsellers #25955

Closed
@clion007

Description

@clion007

Preconditions (*)

  1. Magento version: 2.3.3
  2. PHP version: 7.1.29; Apache version: 2.4.39; Mysql version: 5.7.26

Steps to reproduce (*)

  1. develop a plugin for block of product toolbar to sort product connection by sold quantity use joinleft method of Magento\Framework\DB\Select as below:

$toolbarCollection
->getSelect()
->joinLeft(
'sales_order',
'e.entity_id = sales_order.entity_id',
array('qty_ordered'=>'sales_order.total_qty_ordered'))
->group('e.entity_id')
->order('qty_ordered desc');

  1. flush the product page, the total number of connection items display only 1, not the true number, and don't get the right pagination.

Expected result (*)

  1. fix the bug and display the true number in amount.phtml;
  2. get the right pagination.

I try to fix it override the method of protected function _getSelectCountSql in file \Magento\Catalog\Model\ResourceModel\Product\Collection like below in a new module:

protected function _getSelectCountSql( ?\Magento\Framework\DB\Select $select = null , $resetLeftJoins = true )
{
$this -> _renderFilters();
$countSelect = $this -> _buildClearSelect( $select );
if ( count( $countSelect ->getPart( \Zend_Db_Select::GROUP ) ) > 0 ) {
$countSelect ->reset( \Zend_Db_Select::GROUP );
}
$countSelect -> columns( 'COUNT(DISTINCT e.entity_id)' );
if ( $resetLeftJoins ) {
$countSelect -> resetJoinLeft();
}
return $countSelect;
}

But it does not work.

Actual result (*)

  1. only get 1 item of product connection number;
  2. can't get the right total page number and display the real pagination.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Component: CatalogEvent: mm20inIssue: Format is validGate 1 Passed. Automatic verification of issue format passedIssue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentProgress: doneReproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branchTriage: Dev.ExperienceIssue related to Developer Experience and needs help with Triage to Confirm or Reject it

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions