문제

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

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top