0

I'm using magento 2.3, may i ask how can i made product name on others page. Beacuse i tried this code but not working. Anyone can help? thank you!!

<?php echo $_product->getName(); ?>
12
  • ok so you need any product name anywhere? – sarvesh Dineshkumar Patel Mar 17 '20 at 6:48
  • try this magento.stackexchange.com/questions/307041/… – Msquare Mar 17 '20 at 6:57
  • I just want to display same product name on product view page on footer part, look like breadcrumbs, can you help, thank you! @sarveshDineshkumarPatel – niceonelee Mar 17 '20 at 7:03
  • I just want to display same product name on product view page on footer part, look like breadcrumbs, can you help, thank you! @Msquare – niceonelee Mar 17 '20 at 7:04
  • okay so you want in footer, can you provide me file name? – sarvesh Dineshkumar Patel Mar 17 '20 at 7:04
2

Please you get by-product Id.

$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

Or try for the current page.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$appState = $objectManager->get('\Magento\Framework\App\State');

$registry = $objectManager->get('\Magento\Framework\Registry');
$currentProduct = $registry->registry('current_product');
echo $currentProduct->getName() . '<br />';

I hope it is useful to you.

4
  • I just want to display same product name on product view page on footer part, look like breadcrumbs, can you help, thank you! – niceonelee Mar 17 '20 at 7:04
  • yes just use my code and set that in a variable and put it as PHP code. – sarvesh Dineshkumar Patel Mar 17 '20 at 7:06
  • but not a one product, is want to all product, auto to change. if just setting one Product Id just display one product name. – niceonelee Mar 17 '20 at 7:10
  • ok try second code so you get exact – sarvesh Dineshkumar Patel Mar 17 '20 at 7:12
0

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

block and phtml link like this (add your block file path )

<block class="VendoreName\ModuleName\Block\YourBlockName" name="custom.data" template="VendoreName_ModuleName::yourfilname.phtml" >

Add this code your xml file may be help :-

<referenceContainer name="footer">
            <block class="VendoreName\ModuleName\Block\YourBlockName" name="custom.data" template="VendoreName_ModuleName::yourfilname.phtml" after="-"/>
</referenceContainer>
2
  • I just want to display same product name on product view page on footer part, look like breadcrumbs, can you help, thank you! – niceonelee Mar 17 '20 at 7:05
  • check my update answer may be help you – Mohit Patel Mar 17 '20 at 7:11
0

app\code\VendoreName\ModuleName\Block

ProductName.php

<?php

namespace VendoreName\ModuleName\Block;

use Magento\Framework\App\Request\DataPersistorInterface;

class ProductName extends \Magento\Framework\View\Element\Template
{
    protected $registry;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\Registry $registry,
    ) {
        parent::__construct($context);
        $this->registry = $registry;
    }
    public function getCurrentProduct()
    {
        return $this->registry->registry('current_product');
    }

}

app\code\VendoreName\ModuleName\view\frontend\templates

getProduct.phtml

$prd = $block->getCurrentProduct();
$product_entityid = $prd->getEntityId();
$product_sku = $prd->getSku();
$product_name = $prd->getName();
echo "<h1>".$product_name."</h1>";

app\code\VendoreName\ModuleName\view\frontend\layout

catalog_product_view.xml

<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <body>
      <referenceContainer name="content">
        <referenceContainer name="columns">
            <block class="VendoreName\ModuleName\Block\ProductName" name="custom.data" template="VendoreName_ModuleName::getProduct.phtml" after="-" >
        </referenceContainer>
    </referenceContainer>
    </body>
</page>

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.