Shopping cart

How to get related, upsell & crosssell products in Magento 2

Related, upsell and crosssell products in Magento 2 are the product types you can use for the marketing campaign to boost online. To the watching products, the related products are the items with the similar functions, and the upsell products are the higher version than that. How about the crosssell products? They are the accompanied products for the perfect using. In this topic, you can follow how to get all of them via the code script.

Overview

Step 1: Declare in Mageplaza_HelloWorld Block

You will use a block class of the module Mageplaza_HelloWorld, then possibly inject the object of \Magento\Framework\Registry in the constructor of the module’s block class.

app/code/Mageplaza/HelloWorld/Block/HelloWorld.php

<?php
namespace Mageplaza\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
    protected $_registry;
    
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Framework\Registry $registry,
        array $data = []
    )
    {        
        $this->_registry = $registry;
        parent::__construct($context, $data);
    }
    
    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }
    
    public function getCurrentProduct()
    {        
        return $this->_registry->registry('current_product');
    }    
    
}
?>

In the step 2, you will apply Object Manager for the customize of the HelloWorld class. By this way, allow calling the current product information, then the current product’s related, upsell and crosssell products are gotten one by one.

$myBlock = \Magento\Framework\App\ObjectManager::getInstance()->get('Mageplaza\HelloWorld\Block\HelloWorld');
 
$currentProduct = $myBlock->getCurrentProduct();
 
if ($currentProduct = $myBlock->getCurrentProduct()) {
    $relatedProducts = $currentProduct->getRelatedProducts();
    
    if (!empty($relatedProducts)) {
        echo 'Related Products <br />';    
        foreach ($relatedProducts as $relatedProduct) {
            echo $relatedProduct->getSku() . '<br />';  
            echo $relatedProduct->getName(); //get name
            echo $relatedProduct->getPrice(); //get price
            echo $relatedProduct->getData(); //Show all attributes      
        }
    }    
   
}

Step 3: Get upsell products

$myBlock = \Magento\Framework\App\ObjectManager::getInstance()->get('Mageplaza\HelloWorld\Block\HelloWorld');
$currentProduct = $myBlock->getCurrentProduct();
 
if ($currentProduct = $myBlock->getCurrentProduct()) {
    $upSellProducts = $currentProduct->getUpSellProducts();

    if (!empty($upSellProducts)) {
        echo '<h3>UpSell Products</h3>';
        foreach ($upSellProducts as $upSellProduct) {
            echo $upSellProduct->getSku() . '<br />';        
        }
    }
    
   
}

Step 4: Get crosssell products

$myBlock = \Magento\Framework\App\ObjectManager::getInstance()->get('Mageplaza\HelloWorld\Block\HelloWorld');
$currentProduct = $myBlock->getCurrentProduct();
 
if ($currentProduct = $myBlock->getCurrentProduct()) {
    $crossSellProducts = $currentProduct->getCrossSellProducts();

    if (!empty($crossSellProduct)) {
        echo 'CrossSell Products <br />';
        foreach ($crossSellProducts as $crossSellProduct) {
            echo $crossSellProduct->getSku() . '<br />';        
        }
    }
    
   
}

If that you have any queries about the article or any questions in general, use the comment section below!





Enjoyed the tutorial? Spread it to your friends!

magento-2-tutorial
how-to
get
related,
upsell
&
crosssell
products

Brian Tran
Brian Tran
Senior Magento Developer who is passionate about development and love technology

Comments for How to get related, upsell & crosssell products in Magento 2

Please leave comments if you have any questions, feedbacks.

Module Development

We recommend:

You also may like these Magento 2 Extensions

One Step Checkout

$199
120 reviews

Layered Navigation

$99
61 reviews

Gift Card

$199
8 reviews

Reward Points

$99
8 reviews

Affiliate

$149
16 reviews

Shop By Brand

$99
19 reviews

People also searched for:

  • magento 2 how to get related
  • upsell & crosssell products
  • magento 2 get related
  • upsell & crosssell products
  • get related product magento 2
  • get upsell product in magento 2
  • get crosssell product magento 2