In Magento 2, I try to change number of products in category pages dynamically.

For exemple categories 1 and 2 will have default number value (10), and category 3 and 4 will have one less product, only 9 products per pages.

I will use a custom module attribute to identify which category need less product per page.

Do you know how can achieve this ?


  • @Chikku This is not what I want. I have to make this change dynamically and programmatically for specific categories only. Not for all categories. And I don't have toolbar in my frontend page.
    – Fbr
    Jul 8, 2020 at 9:53

2 Answers 2


The pagination limit are retrieved using function getAvailableLimit in class Magento\Catalog\Block\Product\ProductList\Toolbar. You can use plugin to define your custom limits for pagination.


<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
   <type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
      <plugin name="page_limit"
            sortOrder="2" disabled="false" />

Plugin Vendor\Module\Plugin\Toolbar


 namespace Vendor\Module\Plugin;

 class Toolbar
   * Core registry
   * @var \Magento\Framework\Registry
    protected $_coreRegistry;

    public function __construct(
       \Magento\Framework\Registry $registry
    ) {
       $this->_coreRegistry = $registry;

    public function afterGetAvailableLimit(
      \Magento\Catalog\Block\Product\ProductList\Toolbar $subject,
   ) {
       $category = $this->_coreRegistry->registry('current_category');

       //just an example, you can replace with your code.
       if ($category->getId() == '5') {
          $result = [];
          for ($i = 9, $k = 0; $k < 5; $i = $i+5, $k++) {
            $result[$i] = $i;

       return $result;
  • Thanks for your answer. What is the 5 in this line ? for ($i = 9, $k = 0; $k < 5; $i = $i+5, $k++) {
    – Fbr
    Jul 8, 2020 at 12:37
  • that is just an example I'm using to create page limit with gap of 5 as [ 9, 14, 19, 24, 29] something like this. you can replace it with your code. Jul 8, 2020 at 12:56
  • @AfreenSheikh It works for me thanks for that will you please tell me how to set default number of product show per page how to do that.
    – Prits
    Jun 29, 2021 at 12:21

You can create an observer for this and set page limit dynamic according to your requirement

<event name="catalog_block_product_list_collection">
        <observer name="vendor_module_product_list_collection"
          instance="vendor\Module\Observer\ListCollectionObserver" />

And below are the observer file code ListCollectionObserver. You just need to get current category ID and add your product limit in collection




namespace vendor\module\Observer;

use Magento\Customer\Model\Session;

class ListCollectionObserver implements \Magento\Framework\Event\ObserverInterface

    protected $_storeManager;  

    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
    ) {
        $this->_storeManager = $storeManager;

    public function execute(\Magento\Framework\Event\Observer $observer)

        $productCollection = $observer->getCollection();

        if(isset($_GET['product_list_limit']) && $_GET['product_list_limit']!='all'){
        }if(isset($_GET['product_list_limit']) && $_GET['product_list_limit']=='all'){
            $productCollection->setPageSize($this->scopeConfig->getValue('catalog/frontend/grid_per_page', \Magento\Store\Model\ScopeInterface::SCOPE_STORE))->load();
  • thanks for your answer, but when we set setPageSize($productCollection->setPageSize($limit)->load();) for every page like 1st page - 10, second page - 12 etc, it's skip 2 products from 1st page should be added in next page, it's not done and category missed two products. So please share, how can we manage the same?
    – shyopat
    Aug 25, 2022 at 12:56
  • Hello @shyopat, Do you got any ans for above requirment. Jun 27 at 9:31

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.