1

For some specific categories we would like to change the default value for Products per Page on Grid Default Value. By default it's set to 24.

In our custom theme we create a custom page_layout with the following code. But sadly it does not work, it still use the default value 24. What are we missing here?

<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_layout.xsd">
    <update handle="2columns-left"/>
    <referenceContainer name="content">
        <referenceBlock name="product_list_toolbar">
            <action method="setDefaultGridPerPage">
                <argument name="limit" xsi:type="string">23</argument>
            </action>
        </referenceBlock>
    </referenceContainer>
</layout>

Edit:

When we create a Custom Layout Update for the specific category, it does work well with the following code. But we prefer to add it to the category layout itself, instead of creating a custom. layout update for each category. How can we achieve that?

Custom Layout Update code:

<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <referenceBlock name="product_list_toolbar">
                <action method="setDefaultGridPerPage">
                    <argument name="limit" xsi:type="string">23</argument>
                </action>
            </referenceBlock>
        </referenceContainer>
    </body>
</page>
3
  • Please provide your complete code along with the file path and try this solution. : magento.stackexchange.com/a/371078/82670
    – Msquare
    Commented Dec 4, 2023 at 20:46
  • Have you checked the solution below, or did you encounter any errors?
    – Msquare
    Commented Dec 5, 2023 at 10:31
  • @Msquare Thanks! Not yet, will test it asap
    – JGeer
    Commented Dec 5, 2023 at 19:15

1 Answer 1

2

Please try below code

app/code/VendorName/ModuleName/etc/frontend

di.xml

<?xml version="1.0"?>

<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_custom_product" type="VendorName\ModuleName\Plugin\Toolbar" sortOrder="1" disabled="false" />
   </type>
</config>

app/code/VendorName/ModuleName/Plugin

Toolbar.php

<?php

namespace VendorName\ModuleName\Plugin;

class Toolbar
{

 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');
    //you can replace with your code.
    if ($category->getCustomContentPosition()) { // you can update condition 
       $result = [];
       for ($i = 6, $k = 0; $k < 5; $i = $i+5, $k++) {
         $result[$i] = $i;
       }
    }

    return $result;
  }
}

Here, you have to return valid array values that support pagination; otherwise, it takes default values. You can modify the dynamic array logic or pass your static array into the plugin array above.

OUTPUT enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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