1

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

/vendor/magento/module-catalog/Block/Product/ProductList/Toolbar.php

to

/app/code/Vendor/Module/Rewrite/Magento/Catalog/Block/Product/ProductList/Toolbar.php

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;

    $this->_collection->setCurPage($this->getCurrentPage());

    // we need to set pagination only if passed value integer and more that 0
    $limit = (int)$this->getLimit();
    if ($limit) {
        $this->_collection->setPageSize($limit);
    }
    if ($this->getCurrentOrder()) {
        if ($this->getCurrentOrder() == 'position') {
            $this->_collection->addAttributeToSort(
                $this->getCurrentOrder(),
                $this->getCurrentDirection()
            );
        } else {
            $this->_collection->addAttributeToSort('featured_category','asc');
            $this->_collection->addAttributeToSort(
                $this->getCurrentOrder(),
                $this->getCurrentDirection()
            );
        }
    }
    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

1

You can fetch collection count by the following methods.

$collection->getSize();

OR

$collection->count();

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

$collection->setPageSize($pageSize);
$collection->setCurPage($page);
$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->getSelect()->limit($pageSize,$offset);
$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.
4
  • 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 that you have read and understand our privacy policy and code of conduct.

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