Magento 2.3.5 - Display block on product page only if product got related products
-
14-04-2021 - |
Question
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?
Solution
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(); ?>
<?php
$relatedProductIds = $_product->getRelatedProductIds();
//checke product have related product or not
if (!empty($relatedProductIds)) {
//call the block here...
}
?>
I hope this is helpful to you!!
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange