2

I am trying to get up-sell product in Magento 2 via objectManager. Current product id and name is returning well but up-sell product return empty.

<?php 
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
    echo $product->getId();
    echo $product->getName();
    //echo $currentProduct->getSku();
if ($currentProduct = $product->getCurrentProduct()) {
    $upSellProducts = $currentProduct->getUpSellProducts();
    print_r($upSellProducts);

    if (!empty($upSellProducts)) {
        echo '<h3>UpSell Products</h3>';
        foreach ($upSellProducts as $upSellProduct) {
            echo $upSellProduct->getSku() . '<br />';        
        }
    }
}
?>

enter image description here

1

By Factory Method:

protected $_registry;

public function __construct(
    ...
    \Magento\Framework\Registry $registry,
    ...
) {
    $this->_registry = $registry;
}

public function getCurrentProduct()
{
    return $this->_registry->registry('current_product');
}

public function getUpsellProducts()
{
    $currentProduct = $this->getCurrentProduct();
    $upsellProducts = $currentProduct->getUpSellProducts();

    return $upsellProducts;
}

By ObjectManager:

<?php 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$currentProduct = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product

if ($currentProduct) {

    $upsellProducts = $currentProduct->getUpSellProducts();

    if (!empty($upsellProducts)) {
        echo '<h3>UpSell Products</h3>';
        foreach ($upsellProducts as $upsellProduct) {
            echo $id = $upSellProduct->getSku();
        }
    }
}

Note: With this code, you cannot get full model of product. For that, you need to load product model by upsell product Id.

  • Well great@@ but this code only fetch up-sell product sku, it's does not show full product – sanjay Jun 8 '17 at 9:15
  • Prince Patel@ you there? – sanjay Jun 8 '17 at 9:17
  • @sanjay you can get all data from $product object, check my answer – Prince Patel Jun 8 '17 at 9:22
  • Prince Patel@ please see the updated question after use your code@ you are awesome doing great. but I am looking up-sell product with default view . – sanjay Jun 8 '17 at 9:29
  • Prince Patel@youthere? – sanjay Jun 8 '17 at 9:41
0
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
echo $product->getId();
echo $product->getName();
//echo $currentProduct->getSku();
if ($currentProduct = $product) {
    $upSellProducts = $currentProduct->getUpSellProducts();
    //print_r($upSellProducts);

    if (!empty($upSellProducts)) {
        echo '<h3>UpSell Products</h3>';
        foreach ($upSellProducts as $upSellProduct) {
            echo $upSellProduct->getSku() . '<br />';        
        }
    }
}

Your Answer

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

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