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