I want to know the count of the first sort of listing product in categories. I override




to be able to add another sort. The actual code is:

 * Set collection to pager
 * @param \Magento\Framework\Data\Collection $collection
 * @return $this
public function setCollection($collection)
    $this->_collection = $collection;


    // we need to set pagination only if passed value integer and more that 0
    $limit = (int)$this->getLimit();
    if ($limit) {
    if ($this->getCurrentOrder()) {
        if ($this->getCurrentOrder() == 'position') {
        } else {
    return $this;

I want to know the "featured_category" count to validate the maximum number of products for this sort.

Any ideas, Merry Christmas

1 Answer 1


You can fetch collection count by the following methods.




You can fetch collection count while using pagination in the following way.

$collection->clear(); // Clear your collection.
$collection->getSize();// Give you correct count ignoring above pagination.

You can fetch collection count while using limit & offset in the following way.

$collection->clear(); //This clear collection & give you correct collection total.**  
$collection->getSize(); //Now you will get correct collection total even if you have pagination code.
  • Thanks, but i need the count of the first sort $this->_collection->addAttributeToSort('featured_category','asc'); products count with Attribute "featured_category" value true, is it possible with addAtributeToSort method? Dec 27, 2021 at 13:52
  • According to your above comment featured_category attribute has value like true/false, Yes/No in the backend? can you confirm the attribute value? Basically addAtributeToSort works on numeric attribute values only. Dec 28, 2021 at 11:27
  • Yes, the attribute has value true/false, however this works, but i need maximum number of items with value true (in first sort), i wiil try with numeric attribute value and i will add the attribute to "Products in Category" column. I will bring new comments Dec 29, 2021 at 16:47
  • I had created an numeric attribute and it works! Thanks Feb 7, 2022 at 16:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.