Magento: Set LIMIT on collection

Magento: Set LIMIT on collection

October 10, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: Set LIMIT on collection

Working with Magento Collections: Setting a Limit for Any Collection

As a senior developer working extensively with e-commerce platforms like Magento, I frequently encounter questions regarding collection manipulation. One such query that often arises is how to set a limit on any Magento collection, not just the catalog.

Understanding Magento Collections

Before diving into the solution, let’s briefly recap what collections are in Magento:

Collections represent aggregated data from one or more databases tables. They allow you to retrieve and manipulate multiple datasets simultaneously, making your code more efficient and maintainable.

The Challenge: Limiting Non-Catalog Collections

The official documentation provides a straightforward method for limiting catalog collections using setPage(). However, when working with custom collections like order history, wishlists, or other non-catalog data, this method falls short. The question remains—how do we apply pagination to these collections?

A Comprehensive Solution

To set a limit on any collection in Magento, regardless of its type, you can use the setSize() and getItems() methods together with the Zend_Paginator library.

Here’s an example showcasing this approach for a custom order history collection:

// Assuming $collection is your instance of the custom collection class
$pager = new Zend_Paginator($collection->getItems());

// Set the current page and items per page
$pager->setCurrentPageNumber((int) $this->getRequest()->getParam('page'));
$pager->setItemCountPerPage((int) $this->getRequest()->getParam('limit', 10));

// Get the paginated items
$items = $pager->getCurrentItems();

In this code snippet, we first create a new Zend_Paginator instance using our custom collection’s data. We then set the current page and items per page using the request parameters or default values.

Finally, we retrieve the paginated items using getCurrentItems(). This method returns an array of items limited by the specified pagination settings.

Incorporating into a Controller Action

To use this solution in a controller action, you might have code that looks like this:

public function viewOrderHistoryAction()
{
    $this->_title->set(__('My Order History'));

    // Assuming $collection is an instance of your custom order history collection
    $pager = new Zend_Paginator($collection->getItems());

    // Set pagination parameters
    $pager->setCurrentPageNumber((int) $this->getRequest()->getParam('page'));
    $pager->setItemCountPerPage((int) $this->getRequest()->getParam('limit', 10));

    // Pass the paginated items to the view
    $this->loadLayout();
    $this->renderLayout();

    $this->_view->orderHistory = $pager->getCurrentItems();
}

In this example, we first create a new Zend_Paginator instance and set the pagination parameters. We then pass the paginated items to the view using $_view->orderHistory.

Conclusion

Setting a limit on any Magento collection is possible by leveraging the Zend_Paginator library in combination with the setSize() and getItems() methods. This approach provides a flexible solution for pagination, applicable beyond the catalog collections.

Remember to adjust the example code according to your specific implementation and data retrieval requirements.

If you’re looking to explore more advanced Magento development techniques or want to learn from experienced professionals, consider visiting Laravel Company, where we offer expert guidance on various aspects of e-commerce development.

Catalog Database Themes Configuration

Our network