0

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 ?

Thanks

2
  • @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

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.

etc/frontend/di.xml

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

Plugin Vendor\Module\Plugin\Toolbar

<?php


 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,
      $result
   ) {
       $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;
     }
 }
3
  • 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
1

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" />
</event>

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

$observer->getCollection()->setPageSize(15);

ListCollectionObserver

<?php

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'){
            $productCollection->setPageSize($_GET['product_list_limit'])->load();
        }if(isset($_GET['product_list_limit']) && $_GET['product_list_limit']=='all'){
        $productCollection->load();
        }else{
            $productCollection->setPageSize($this->scopeConfig->getValue('catalog/frontend/grid_per_page', \Magento\Store\Model\ScopeInterface::SCOPE_STORE))->load();
        }
        if(isset($_GET['p'])){
            $productCollection->setCurPage($_GET['p'])->load();
        }else{
            $productCollection->setCurPage(1)->load();
        }               
    }
}
2
  • 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.