Magento 2, Cross-sell product will be display in Shopping Cart page. By Native Magento, Only four Cross-sell product will be display in cart page. If you want to increase or decrease the Cross-sell Product display limit in cart page you need to modify Core Crosssell.php file.
Native file location for CrossSell Product, Magento\Checkout\Block\Cart\Crosssell
You can check the hardcoded product limit by protected $_maxItemCount = 4;
If you want to change the limit you need to override $_maxItemCount variable.
Never modify the core file in Magento.
To override variable you need to create a module for changes.
I have kept Rbj as Packagename and CrossSell as Module name.
Create a Module called Rbj_CrossSell.
Create registration.php and module.xml to declare our module.
File Path, app/code/Rbj/CrossSell/registration.php
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Rbj_CrossSell', __DIR__ );
File Path, app/code/Rbj/CrossSell/etc/module.xml We have added the dependency on Native CrossSell module.
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Rbj_CrossSell" setup_version="1.0.0"> <sequence> <module name="Magento_Checkout"/> </sequence> </module> </config>
Create di.xml file for an override core file,
File: app/code/Rbj/CrossSell/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"> <preference for="Magento\Checkout\Block\Cart\Crosssell" type="Rbj\CrossSell\Block\Cart\Crosssell" /> </config>
Now Create Block file,
Create app/code/Rbj/CrossSell/Block/Cart/Crosssell.php
<?php namespace Vendor\Module\Block\Checkout\Block\Cart; use Magento\CatalogInventory\Helper\Stock as StockHelper; class Crosssell extends \Magento\Checkout\Block\Cart\Crosssell { public function __construct( \Magento\Catalog\Block\Product\Context $context, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Catalog\Model\Product\Visibility $productVisibility, \Magento\Catalog\Model\Product\LinkFactory $productLinkFactory, \Magento\Quote\Model\Quote\Item\RelatedProducts $itemRelationsList, \Magento\CatalogInventory\Helper\Stock $stockHelper, array $data = [] ) { parent::__construct( $context, $checkoutSession, $productVisibility, $productLinkFactory, $itemRelationsList, $stockHelper, $data ); $this->_maxItemCount = 10; // set your product limit here } }
Run Command to Install Our module from a root of your Magento instance,
php bin/magento setup:upgrade php bin/magento setup:static-content:deploy -f php bin/magento setup:di:compile php bin/magento cache:flush