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(); ?>
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(); ?>
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.
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>
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>