0

Is it possible to call out the related products on category pages? So within each product in the grid, it would also display the full list of that product's related products.

Related products on category

0

Please try below code on your phtml file.

$product_id = '1';
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($product_id);

$relatedProducts = $product->getRelatedProducts();

//var_dump($relatedProducts);

if (!empty($relatedProducts)) {
    echo 'Related Products <br />';   
    foreach ($relatedProducts as $relatedProduct) {
        $_product = $objectManager->create('Magento\Catalog\Model\Product')->load($relatedProduct->getId());
        echo $relatedProduct->getId().'<br/>';
        echo $_product->getPrice().'<br/>';
        echo $_product->getName().'<br/>';
        echo $relatedProduct->getId(); //get name
        echo "<br>";
    }
}  

|improve this answer|||||

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.