0

Note : First of all it is not good practice to use object manager directly on any file.use magento standard only

Below code is just to show that how we can easily get the upsell product on category listing page.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');
$categoryId = $category->getId();
$categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');

$category = $categoryFactory->create()->load($categoryId);
$categoryProducts = $category->getProductCollection()
                         ->addAttributeToSelect('*');

foreach ($categoryProducts as $product) {
    $current_product_id = $product->getId();
    $current_product = $objectManager->create('Magento\Catalog\Model\Product')->load($current_product_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 'Upsell Product name---'.$product->getName().'<br /><br/>';
            }
        }
    } 
}
  • it is a question Or Answer ? – Pawan Apr 9 at 6:31
  • i was suppose to ask question but after that i have debug and found solution so i have shared – John Apr 9 at 7:03
  • OK, got it... :) – Pawan Apr 9 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.