Magento 2 – How to Increase/Decrease Cross-Sell Products Limit
cross-sellsmagento-2.1magento-2.1.7magento2
I'm trying to increase the cross-sell products on the cart page up to 5.
In Magento-1, this is possible by overriding a checkout block (or changing a var in the core). I've been trying to use something similar to do this in Magento-2 but I am unable to produce any results.
Could someone please help me with increasing the limit.
Best Answer
You can easily change following variable in Magento/Checkout/Block/Cart/Crosssell.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,
StockHelper $stockHelper,
array $data = []
) {
parent::__construct(
$context,
$checkoutSession,
$productVisibility,
$productLinkFactory,
$itemRelationsList,
$stockHelper,
$data
);
// this variable you can change what you need
$this->_maxItemCount = 8;
}
}
above extension add extra page after cart and that page you will add these code and you will get cross-sell product data
change your indexController indexAction as below
public function indexAction()
{
$this->loadLayout()
->_initLayoutMessages('checkout/session')
->_initLayoutMessages('catalog/session');
$_product = Mage::getModel('catalog/product')->load(1); //here 1 is product Id
$block = $this->getLayout()->createBlock('checkout/cart_crosssell')->setProduct($_product)->setTemplate('checkout/cart/crosssell.phtml');
$this->getLayout()->getBlock('content')->append($block);
$this->renderLayout();
}
and for your 4 product issue please check in your app/design/frontend/YourPackge/YourTheme/checkout/cart/crosssell.phtml file any condition who breaks for loop after four product then change it.
to increase limit for cross-sell product follow this quick step
1) copy app/code/core/Mage/Checkout/Block/Cart/Crosssell.php file to app/code/local/Mage/Checkout/Block/Cart/Crosssell.php
2) find this line and increase limit as per your need protected $_maxItemCount = 4;
Best Answer
You can easily change following variable in Magento/Checkout/Block/Cart/Crosssell.php
Check more detail here
NB: Don't modify core file.
[Update]
Make sure your module depends on 'Magento_Checkout' module in module.xml