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

Related, upsell and cross-sell products are the product types you can use for the marketing campaign to boost online. In this topic, I will show you how to get all of them via the code script.

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!

Magento 2 Auto Related Products

Enjoyed the tutorial? Spread it to your friends!

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

Sam Thomas
Sam Thomas

CEO and Founder of Mageplaza. Pursueing a simple and healthy lifestyle. A friend, a husband and a dad of two children, a trainer and an influencer wannabe. He is a big fan of sports and travel, also.

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