Magento 2 - Set collection limit in a Model

Magento 2 - Set collection limit in a Model

August 27, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2 - Set collection limit in a Model

Setting Collection Limits in Magento 2 Models

Hello developers! Today we’re diving into a common challenge in Magento 2 - limiting the number of products returned from a model. Let’s tackle this issue step-by-step.

Understanding the Current Approach

You’ve created a custom model Test\Ice\Model\Data to fetch products with a limit. Here’s your current setup:

    <?php

namespace Test\Ice\Model;

class Data extends \Magento\Framework\App\Helper\AbstractHelper {

/**
 * System configuration values
 *
 * @var array
 */
protected $_config;

/**
 * Store manager interface
 *
 */
protected $_storeManager;

/**
 * Product interface
 *
 */
protected $_product;

/**
 * Initialize
 *
 * @param Magento\Framework\App\Helper\Context $context
 * @param Magento\Catalog\Model\ResourceModel\Product\Collection $collection
 * @param Magento\Store\Model\StoreManagerInterface $storeManager
 * @param Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
 * @param array $data
 */
public function __construct(
    \Magento\Framework\App\Helper\Context $context,
    \Magento\Catalog\Model\ResourceModel\Product\Collection $collection,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    array $data = []
) {
    $this->_product = $collection;
    $this->_storeManager = $storeManager;
    parent::__construct($context, $data);
}

public function getProducts() {

    $limit = 10;

    return $this->_product->getSelect()->limit($limit);

}

Your getProducts() method attempts to limit the collection by calling $_product->getSelect()->limit($limit), which you expect would restrict the output to your defined limit of 10.

The Issue: Why the Current Approach Fails

The problem lies in how Magento’s collections work. When you inject a collection into your model using dependency injection, it creates a new instance every time. This means that when you call $_product->getSelect()->limit($limit), it doesn’t limit the original Magento collection; it limits the temporary collection created for this instance.

The Solution: Correctly Limiting the Collection

To properly limit the collection in your model, you need to fetch the data from the Magento collection before applying any filters or limitations. Here’s how to do it:

  1. Inject the product resource model instead of a collection.
  2. Create a new collection and apply your filters there.
  3. Limit the collection.
  4. Fetch the data.
    <?php

namespace Test\Ice\Model;

class Data extends \Magento\Framework\App\Helper\AbstractHelper {

/**
 * Store manager interface
 *
 */
protected $_storeManager;

/**
 * Product resource model
 *
 */
protected $_productResource;

/**
 * Initialize
 *
 * @param Magento\Framework\App\Helper\Context $context
 * @param Magento\Catalog\Model\ResourceModel\Product $resource
 * @param Magento\Store\Model\StoreManagerInterface $storeManager
 * @param array $data
 */
public function __construct(
    \Magento\Framework\App\Helper\Context $context,
    \Magento\Catalog\Model\ResourceModel\Product $resource, 
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    array $data = []
) {
    $this->_productResource = $resource;
    $this->_storeManager = $storeManager;
    parent::__construct($context, $data);
}

public function getProducts() {

    $limit = 10;

    // Create a new collection
    $collection = $this->_productResource->getCollection();

    // Apply any filters (optional)
    // Example: $collection->addFieldToFilter('status', \Magento\Catalog\Model\Product\AttributeSet::STATUS_ENABLED);

    // Limit the collection
    $collection->setPageSize($limit);
    $collection->setCurPage(1);

    return $collection;
}

Now, when you call your getProducts() method, it will correctly limit the Magento product collection to 10 items.

When to Use This Approach

This method is particularly useful when you need to fetch a specific number of products in a custom model. It allows for fine-grained control over which products are returned without modifying the core or using complex query builders.

Conclusion

In this post, we explored the challenges and solutions for setting collection limits in Magento 2 models. By understanding how Magento collections work and injecting the resource model instead of the collection directly, you can achieve the desired result with precision and efficiency.

Remember, always aim to use Magento’s built-in tools and patterns when possible. This ensures your code remains compatible with future updates and performs optimally within the platform’s architecture.

For more advanced techniques in Magento development, feel free to explore our blog at LaravelCompany. We provide regular tips, tricks, and best practices for leveraging Magento to its full potential. Until next time, keep coding!

Magento 2 Catalog Configuration Errors

Our network