5

Hi Everyone i want to set Product Limit in Related and Upsell Product in View page Magento 2.

Any one have idea to set limit.

4 Answers 4

9

You can also use the layout if you don't want to create a plugin.

Assuming you have a theme "Vendor_default" and running Magento >= 2.1.2

Upsell Products:

  • Create the file app/design/frontend/Vendor/default/Magento_Catalog/layout/catalog_product_view.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="product.info.upsell">
            <action method="setItemLimit">
                <argument name="type" xsi:type="string">upsell</argument>
                <argument name="limit" xsi:type="string">5</argument>
            </action>
        </referenceBlock>
    </body>

Related Products:

  • Copy/paste vendor/magento/module-catalog/view/frontend/templates/product/list/items.phtml to app/design/frontend/Vendor/default/Magento_Catalog/templates/product/list/items.phtml

  • In case 'related' block, Change $limit. (ie: $limit = 0 into $limit = 4)

5

You need to use the plugin system of Magento 2.

The objective of plugin is to add a limit on the product items without overriding the module Magento_Catalog itself with an after plugin.

This is an example for the Related Product List :

  1. Assume that you have an existing module Vendor_Module
  2. Create Vendor/Module/etc/frontend/di.xml file and this block of code :

    <?xml version="1.0" encoding="UTF-8" ?>
    
    <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\Related">
            <plugin name="vendor_module_plugin" type="Vendor\Module\Block\Plugin\Product\ProductList\Related" sortOrder="0" />
        </type>
    </config>
    
  3. Create Vendor/Module/Block/Plugin/Product/ProductList/Related.php and add this code in :

    <?php
    
    namespace Vendor\Module\Block\Plugin\Product\ProductList;
    
    /**
     * Class Related
     *
     * @package Vendor\Module\Block\Plugin\Product\ProductList
     */
    class Related
    {
        /**
         * Add limit to related product collection if page_size argument is defined
         *
         * @param \Magento\Catalog\Block\Product\ProductList\Related $subject
         * @param $result
         *
         * @return \Magento\Catalog\Model\ResourceModel\Product\Collection
         */
        public function afterGetItems(\Magento\Catalog\Block\Product\ProductList\Related $subject, $result)
        {
            $pageSize = 10;
            $collection = $result;
    
            if ($pageSize) {
                $collection->getSelect()->limit($pageSize);
            }
    
            return $collection;
        }
    }
    

You can do the same thing for Upsell product :).

1
2

This is an example for the Upsell Product List :

Create the file app/design/frontend/Vendor/default/Magento_Catalog/templates/product/list/items.phtml

Line No. 62 replace below code:

$limit = $block->getPositionLimit();
to
$limit = 3;

and Line No. 82 replace below code:

 $limit = $block->getItemLimit('upsell'); 
to
$limit = 3;

1
0

This is working solution now and its better for performance because it limits the related products before database load.

1.Vendor/Module/etc/frontend/di.xml

<?xml version="1.0" encoding="UTF-8" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd>
    <type name="Magento\Catalog\Model\Product">
        <plugin name="Vendor_Module::plugin_product" type="Vendor\Module\Plugin\Product\ProductPlugin" sortOrder="0"  />
    </type>
</config>
  1. Create plugin class

    <?php
    
     namespace Vendor\Module\Plugin\Product;
    
     /**
      * Class ProductPlugin
      *
      * @package Vendor\Module\Plugin\Product
      */
     class ProductPlugin
     {
         public function afterGetRelatedProductCollection(\Magento\Catalog\Model\Product $subject, $result)
         {
             $pageSize = 6;
             $collection = $result;
    
             if ($pageSize) {
                 $collection->setPageSize($pageSize);
             }
    
             return $collection;
         }
     }
    

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.