I am trying to get cross sell products in cart page programmatically.

Below code i have used in layout/checkout_cart_index.xml

   <referenceContainer name="content">
        <block class="Vendor\Module\Block\Customcross" name="cross.custom.details" after="-" template="Vendor_Module::cart/cart_products.phtml"/>

So I need to write one function in my Block file that should return cross sell items.

Right now i am using like below in block file.

 namespace Vendor\Module\Block;
 use Magento\Catalog\Api\ProductRepositoryInterface;
 use \Magento\Catalog\Block\Product\AbstractProduct;
 class Customcross extends \Magento\Framework\View\Element\Template
protected $_storeManager;    
protected $_productCollectionFactory;
protected $_coreRegistry;  
protected $_urlInterface;
protected $_productloader;  
protected $_productRepository; 
protected $_productData; 
protected $_itemCollection;
protected $_catalogConfig;

public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Framework\Registry $coreRegistry,
    \Magento\Framework\UrlInterface $urlInterface,
    \Magento\Catalog\Model\ProductFactory $_productloader,
    ProductRepositoryInterface $productRepository,
    \Magento\Catalog\Model\Product $productData,
    \Magento\Catalog\Model\Config $catalogConfig
    $this->_productCollectionFactory = $productCollectionFactory;  
    $this->_storeManager = $storeManager;
    $this->_coreRegistry = $coreRegistry;
    $this->_urlInterface = $urlInterface;
    $this->_productloader = $_productloader;
    $this->_productRepository = $productRepository;
    $this->_productData = $productData;
    $this->_catalogConfig = $catalogConfig;

public function getCurrencySymbol() {
    return $this->_storeManager->getStore()->getBaseCurrency()->getCurrencySymbol();

public function getBasetUrl() {
   return $this->_storeManager->getStore()->getBaseUrl();

public function getCurrentUrl() {

return $this->_urlInterface->getCurrentUrl();

public function getStore()
    return $this->_storeManager->getStore();

public function getProductData($productId)
    $productObj = $this->_productData->load($productId);        
    return $productObj;

protected function _prepareData()
    $product = $this->_coreRegistry->registry('product');
    /* @var $product \Magento\Catalog\Model\Product */

    $this->_itemCollection = $product->getCrossSellProductCollection()->addAttributeToSelect(


    foreach ($this->_itemCollection as $product) {

    return $this;
 public function getItems()
     return $this->_itemCollection;

Anyone help me. How to get the cross sell items like this.

  • cross-sell products by default shows on cart page. why you need to do this again? Nov 10 '18 at 12:00
  • Yes, default will show, but I am trying to get programmatically with some custom design in cart page, so can we get like I requested? Or should use default only? Nov 10 '18 at 12:40
  • @jafarpinjar, you have many typo mistakes in the code. Like use \Magento\Catalog\Block\Product\AbstractProduct; should be use Magento\Catalog\Block\Product\AbstractProduct;. Nov 12 '18 at 9:34
  • If you are using use Magento\Catalog\Block\Product\AbstractProduct in your code, you should inject this class in your constructor. Nov 12 '18 at 9:36
  • okay, so the change from use \Magento\Catalog\Block\Product\AbstractProduct; to use Magento\Catalog\Block\Product\AbstractProduct; will be fine or any class i need to inject? Nov 12 '18 at 9:41

I haven't tested it, but perhaps you can do something similar to vendor/magento/module-catalog/Block/Product/ProductList/Crosssell.php _prepareData() function:

public function getItems()
   $product = $this->_coreRegistry->registry('product');
    /* @var $product \Magento\Catalog\Model\Product */

    return $product->getCrossSellProductCollection()->addAttributeToSelect(
  • Not cleared with the answer, can you pls update full block class code? Nov 12 '18 at 3:10
  • We should get from quote I think instead of current product, because can be many products in cart. Nov 12 '18 at 3:11
  • Hi @Sveta, can you update me full code of clock file Nov 12 '18 at 7:43
  • Tried your code, that didn't work for me Nov 12 '18 at 8:31
  • Please try adding \Magento\Catalog\Model\Config $catalogConfig to your constructor. Nov 12 '18 at 9:14

I have done to create cross cell products in cart page display only for two products.

Path: Vendor/ModuleName/registration.php


path: Vendor/ModuleName/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\ModuleName\Block\Cart\Crosssell" />

path: Vendor/ModuleName/etc/module.xml

<?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="Vendor_ModuleName" setup_version="1.0.1">
            <module name="Magento_Checkout"/>

path: Vendor/ModuleName/Block/Cart/Crosssell.php


namespace Vendor\ModuleName\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 = []
    ) {

        $this->_maxItemCount = 2; // here you can set product display limit

It is working for me, I hope to work for all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.