Domanda

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

È stato utile?

Soluzione

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>";
    }
}  

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top