Magento Cart – How to Increase Number of Crosssell Items
cartcross-sells
I want to increase the number of cross sell items appearing in cart. The max item count is 4 in defined at app/code/core/Mage/Checkout/Block/Cart/Crossell.php
class Mage_Checkout_Block_Cart_Crosssell extends Mage_Catalog_Block_Product_Abstract
{
protected $_maxItemCount = 4;
How can I increase this limit from admin end or set the limit in checkout.xml file.
Please Guide.
Thanks.
Best Answer
You're going to need to implement your own module to do so
app/code/local/MyCompany/Checkout/Block/Cart in Crosssell.php
class MyCompany_Checkout_Block_Cart_Crosssell extends Mage_Catalog_Block_Product_Abstract
{
// /**
// * Items quantity will be capped to this value
// *
// * @var int
// */
// protected $_maxItemCount = 100;
// /**
// * Get crosssell items
// *
// * @return array
// */
public function getItemCount()
{
return count($this->getItems());
}
public function getItems()
{
$items = $this->getData(\'items\');
if (is_null($items)) {
$items = array();
$ninProductIds = $this->_getCartProductIds();
if ($ninProductIds) {
$lastAdded = (int) $this->_getLastAddedProductId();
if ($lastAdded) {
$collection = $this->_getCollection()
->addProductFilter($lastAdded);
if (!empty($ninProductIds)) {
$collection->addExcludeProductFilter($ninProductIds);
}
$collection->setPositionOrder()->load();
foreach ($collection as $item) {
$ninProductIds[] = $item->getId();
$items[] = $item;
}
}
if (count($items) < 100) {
$filterProductIds = array_merge($this->_getCartProductIds(), $this->_getCartProductIdsRel());
$collection = $this->_getCollection()
->addProductFilter($filterProductIds)
->addExcludeProductFilter($ninProductIds)
->setPageSize(100-count($items))
->setGroupBy()
->setPositionOrder()
->load();
foreach ($collection as $item) {
$items[] = $item;
}
}
}
$this->setData(\'items\', $items);
}
return $items;
}
}
app/code/local/MyCompany/Checkout/etc in config.xml:
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;
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;
}
}
Best Answer
You're going to need to implement your own module to do so
app/code/local/MyCompany/Checkout/Block/Cart
inCrosssell.php
app/code/local/MyCompany/Checkout/etc
inconfig.xml
:app/etc/modules
inMyCompany_Checkout.xml
:app/design/frontend/default/mytheme/layout/
incheckout.xml
:EDIT : To make the value selectable in the backend, you'll want to create:
app/code/local/MyCompany/Checkout/etc
insystem.xml
:Add this to your existing
app/code/local/MyCompany/Checkout/etc/
inconfig.xml
:And finally, you'd want to replace
app/code/local/MyCompany/Checkout/Block/Cart
inCrossell.php
:With
If you'd like to know why the config section works like it works, the best article for reference in my opinion is:
Alan Storm | Custom Magento System Configuration
Good luck!