This must be relatively simple to do - but I am really struggling.

I have created a new phtml file 'newfile.phtml' that appears in the catalog\product\view section and displays the word 'hello' on the product page. I want to update this so it includes the product name and the product url. I have tried using the following:

<?php echo $product->getData('name'); ?>

However, nothing is returned with the above.

Can anyone suggest the cleanest way to get current product name from within a standard .phtml file?

Thank you.

  • How do you define $product in your phtml ? You should access it with a block or at least a this object I guess. So you need to look where this block or this object is defined cause it's in that object you miss the data name.
    – Claims
    Commented Aug 25, 2020 at 14:01

3 Answers 3


It will depend on the class you have configured your template to use in the layout file. For example,

<block class="Magento\Catalog\Block\Product\View" template="yourCustom.phtml">

will allow you to retrieve the product name and URL within yourCustom template using:

$product = $block->getProduct();
$productName = $product->getName();
$productUrl = $product->getProductUrl();

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


  protected $product;  

  public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Model\ProductFactory $product
    ) {
        $this->product = $product;

    public function getProduct($id)
        return $this->product->create()->load($id);


Then call getProduct function in your phtml file

$productId = "10"; //Product Id

echo $product->getName(); //Get Product Name

If you want to get in current product information in phtml you can use following code.

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $currentProduct = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product information
    echo $currentProduct>getId();
    echo $currentProduct>getName();

in magento 2 there are view models that allow you to do what you are looking for according to magento 2 best practice. docs: https://devdocs.magento.com/guides/v2.4/extension-dev-guide/view-models.html

follow the guide and implement a custom function using this class

use Magento\Catalog\Model\Session as CatalogSession;


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.