0

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

1 Answer 1

1

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 />';        
        }
    }
}
?>
11
  • How to get price and rating collection for upsell products
    – Jaisa
    Commented Feb 14, 2018 at 10:22
  • Hi @Abishek, as per above code ,i wish to get only the instock upsell product collection like in view page
    – Jaisa
    Commented Jun 13, 2018 at 18:25
  • Do you want to get only in stock upsell products? Commented Jun 13, 2018 at 18:26
  • Yes , like in view page
    – Jaisa
    Commented Jun 13, 2018 at 18:37
  • I wish to add stock filter while getting the collection instead of foreach the all the upsell products and check stock status
    – Jaisa
    Commented Jun 13, 2018 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.