On our productpage we want to display a block, only if that product got related products.

How can I check in M2 if the product got related products?

1 Answer 1


Step - 1 Override the catalog_product_view.xml layout into your theme or your module and call this block.

<block class="Magento\Catalog\Block\Product\View" name="blockname"  template="Vendor_ModuleName::blockname.phtml"/>

Step- 2

Now Create the blockname.phtml into the view\frontend\templates\ directory and write down the below code

<?php $_product = $block->getProduct(); ?>

$relatedProductIds = $_product->getRelatedProductIds(); 
//checke product have related product or not
if (!empty($relatedProductIds)) {
    //call the block here...


I hope this is helpful to you!!

  • Thanks, works perfect!
    – JGeer
    Jul 4, 2020 at 14:15

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.