Question

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.

Was it helpful?

Solution

You can easily change following variable in Magento/Checkout/Block/Cart/Crosssell.php


protected $_maxItemCount = 4;

Check more detail here

NB: Don't modify core file.

[Update]

Make sure your module depends on 'Magento_Checkout' module in module.xml

create Vendor/Module/etc/frontend/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="1.0.0"></module>
        <sequence>
            <module name="Magento_Checkout"/>
        </sequence>
</config>

Create Vendor/Module/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="Vendor\Module\Block\Checkout\Block\Cart\Crosssell" />
</config>

Create Vendor/Module/Block/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;
    }
}

OTHER TIPS

For magento 2 Store > Configuration > Catalog > Catalog > Rule-Based Product Relations > Maximum Number of Products in Cross-Sell Product List

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top