SEO Ultimate package released! Grab 20% off - SEO20 Explore

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.

  • Step 1: Declare in Mageplaza_HelloWorld
  • Step 2: Get related products
  • Step 3: Get upsell products
  • Step 4: Get crosssell products

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

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!



Next tutorial:

Module Development Series



Enjoyed the tutorial? Spread it to your friends!

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

Module Development

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

Please leave comments if you have any questions, feedbacks.

You also may like these Magento 2 Extensions

One Step Checkout

$199
10 reviews

Layered Navigation

$99
12 reviews

SEO

FREE
no review

Shop By Brand

$99
3 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