Question

I have created a module to show upsell products in List page. In List page, I have put one icon on each product, while clicking that icon, It would need to show upsell products list which is associated with the current product. So Please provide me a solution to get the upsell products list Collection, for a particular product in list page. Thanks in advance

Was it helpful?

Solution

Try below code. I won't recommend using objectManager. Instead create function and call your Upsell products on List Page.

<?php 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$current_id = 10; //get current product id. Its should be dynamic each time you click
$current_product = $objectManager->create('Magento\Catalog\Model\Product')->load($current_id);

if ($current_product) {
    $upSellProducts = $current_product->getUpSellProducts();
    if (!empty($upSellProducts)) {
        foreach ($upSellProducts as $upSellProduct) {
            $productId = $upSellProduct->getId();
            $product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId); 
            echo $product->getName().'<br />';
            echo $product->getSku().'<br />';
            echo $product->getProductUrl().'<br />';
            echo $product->getImage().'<br />';        
        }
    }
}
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top