In Magento administration, Related, Upsell, and Cross-sell products can be set for each individual product.
Related and Upsell products are displayed on the product page of those products for which we have set them up. There is no limit to their number, so if an online store administrator enters a larger number of products, all of them can be shown on the page.
On the other hand, Cross-sell products are shown in the cart according to the products that are in it. By default, they are limited to four.
In this article, we will limit the number of Related, Cross-sell and Upsell products shown on the product page and in the cart. If more products are entered in the product settings in the Magento administration, only some of them will be displayed. We will limit the size of the collections used for loading Related and Upsell products. There is already a restriction on Cross-sell products, so we’ll just override it.
In order not to edit Magento core files, we will create a new Optiweb_LimitRelatedCrossUpsell module that will overwrite only the files that are relevant to us.
To add a new module, we create a registration.php and module.xml files.
In the Magento core file Magento\Checkout\Block\Cart\Crosssell.php, the $_maxItemCount variable specifies a limit on the displayed Cross-sell products that appear in the cart. The limit is set to 4 by default. In our module, we create a new file app/code/Optiweb/LimitRelatedCrossUpsell/Block/Cart/Crosssell.php, in which we override the $_maxItemCount variable and assign it a new value.
class Crosssell extends \Magento\Checkout\Block\Cart\Crosssell
* We can now set our limit here
const CROSSSELL_LIMIT = 2;
* Crosssell constructor.
* @param \Magento\Catalog\Block\Product\Context $context
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Magento\Catalog\Model\Product\Visibility $productVisibility
* @param \Magento\Catalog\Model\Product\LinkFactory $productLinkFactory
* @param \Magento\Quote\Model\Quote\Item\RelatedProducts $itemRelationsList
* @param \Magento\CatalogInventory\Helper\Stock $stockHelper
* @param array $data
public function __construct(
array $data = 
$this->_maxItemCount = self::CROSSSELL_LIMIT; //limit number of products
As the limit for Related and Up-sell products is not set by default, we need to override the _prepareData() methods, which create product collections for the currently selected product. We will limit collections with setPageSize().
Cross-sell products by changing the value of the constant CROSSSELL_LIMIT (app/code/Optiweb/LimitRelatedCrossUpsell/Block/Cart/Crosssell.php)
Related products by changing the value of the constant RELATED_LIMIT (app/code/Optiweb/LimitRelatedCrossUpsell/Block/Product/ProductList/Related.php)
Up-sell products by changing the value of the constant UPSELL_LIMIT (app/code/Optiweb/LimitRelatedCrossUpsell/Block/Product/ProductList/Upsell.php)
By applying the steps above we can now successfully configure the number of cross-sell, related and up-sell items. This allows us to have full control over how many items are displayed in each section, which in turn helps us design our ecommerce to our advantage, improve the user experience and consequently increase sales.