Well, here is getSize() definition inside AbstractDb class

public function getSize()
        if ($this->_totalRecords === null) {
            $sql = $this->getSelectCountSql();
            $this->_totalRecords = $this->_totalRecords ?? $this->getConnection()->fetchOne($sql, $this->_bindParams);


        return (int)$this->_totalRecords;

It's clear that if _totalRecords value is null, so it goes to run getSelectCountSql() query to get count and save it in _totalRecords to reload it again once need. I logged the query used to get count and it's similar to the query which already get the product collection however the real collection count is just 2 products which is correct after applying filters, but getSize() returns 6 products which is total products I have.

    2021-05-27T20:43:27+00:00 INFO (6): 6---->SELECT COUNT(DISTINCT e.entity_id) FROM `catalog_product_entity` AS `e`
 INNER JOIN `catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND price_index.customer_group_id = '4' AND price_index.website_id = '1'
 INNER JOIN `ul_current_stock_status` AS `stock_status_index` ON e.entity_id = stock_status_index.product_id AND stock_status_index.sales_entity = '2' WHERE ((stock_status_index.stock_status = 1)) AND (e.created_in <= 1) AND (e.updated_in > 1)

this is the log for the first query ran in the getSize() method. _totalRecords = 6 however when running this query it returns 2 Any clues on what's happening here ?

  • this function is just for check "is collection empty or not?? what u want to achieve? Commented May 28, 2021 at 3:31
  • use collection->count() Commented May 28, 2021 at 6:54
  • 1
    @RizwanKhan $collection->getSize() already used in Toolbar. I figured out that getSize() query returns the correct number but the problem is with _totalRecords which was already populated from CatalogSearch _renderFiltersBefore before reaching getSize() statment. that's a weird logic from Magento
    – Ahmed Atef
    Commented May 28, 2021 at 11:52


Browse other questions tagged or ask your own question.