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? – Ramkishan Suthar 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? – jafar pinjar 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;. – Mohit Kumar Arora 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. – Mohit Kumar Arora 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? – jafar pinjar 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(
| improve this answer | |
  • Not cleared with the answer, can you pls update full block class code? – jafar pinjar Nov 12 '18 at 3:10
  • We should get from quote I think instead of current product, because can be many products in cart. – jafar pinjar Nov 12 '18 at 3:11
  • Hi @Sveta, can you update me full code of clock file – jafar pinjar Nov 12 '18 at 7:43
  • Tried your code, that didn't work for me – jafar pinjar Nov 12 '18 at 8:31
  • Please try adding \Magento\Catalog\Model\Config $catalogConfig to your constructor. – Sveta Oksen Nov 12 '18 at 9:14

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.