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.

Related prducts are the products shown to customers in addition to the items they are viewing.

Up-sell products are the products that have higher price or quality than the items they are looking at.

Cross-sell products are the related or complementary products of the items customers are looking at with the purpose to increase the value of the existing products.

Each type of products can be placed in different positions in a Magento 2 store. They are usually used to boost sales effectively.

Now let’s go into details about how to get these products in Magento 2.

4 Steps to get related, up-sell, and cross-sell products in Magento 2:

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 />';        
        }
    }
    
   
}

Wrap up

That’s all about getting related, up-sell, and cross-sell products in Magento 2. I hope that this is a useful tutorial for you. If that you have any queries about the article or any questions in general, use the comment section below!

Thanks for reading!


automatic related product

Automatic Related Products for M2

Encourage customers to add more items to their cart by showing related products alongside their favorites

Check it out!


Image Description
Sam is the CEO & co-founder of Mageplaza, a company established to support Magento merchants with different powerful tools and resources. Sam Nguyen is also the CEO & founder of Avada Commerce, an e-commerce solution provider headquartered in Singapore – aiming to support more than a million online businesses to grow and develop.

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
  • 2.3.x, 2.4.x
x