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

  • @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 '20 at 9:53
1

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;
     }
 }
  • 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 '20 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. – Afreen Sheikh Jul 8 '20 at 12:56
0

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();
        }               
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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