How to get Product name in phtml file in magento2
You need to load product to get product name
With Object Manager
$productId = "10"; //Product Id
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
echo $product->getName(); //Get Product Name
Note: Don't use objectManager directly
With Factory Method
<?php
protected $product;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Model\ProductFactory $product
) {
$this->product = $product;
parent::__construct($context);
}
public function getProduct($id)
{
return $this->product->create()->load($id);
}
}
Then call getProduct
function in your phtml file
$productId = "10"; //Product Id
$product=$this->getProduct($productId);
echo $product->getName(); //Get Product Name