I am new at Magento, I would like to render the product name to catalog_product_view.xml (product page) from a custom phtml but it didn't work. May I know where did I go wrong? Thanks!
app/code/Vendor/Custom_Module/BlockProductName.php
<?php
namespace Vendor\Custom_Module\Block;
use Magento\Catalog\Model\Product;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Registry;
use Magento\Framework\View\Element\Template;
class Popup extends Template
{
/**
* @var Registry
*/
protected $registry;
/**
* @var Product
*/
private $product;
public function __construct(Template\Context $context,
Registry $registry,
array $data)
{
$this->registry = $registry;
parent::__construct($context, $data);
}
/**
* @return Product
*/
private function getProduct()
{
if (is_null($this->product)) {
$this->product = $this->registry->registry('product');
if (!$this->product->getId()) {
throw new LocalizedException(__('Failed to initialize product'));
}
}
return $this->product;
}
public function getProductName()
{
return $this->getProduct()->getName();
}
}
product.phtml
<?php
/**
* @var Vendor\Custom_Module\BlockProductName $block
*/
$productName = $block->getProductName();
?>
<h1> The product Name: <?php echo $productName; ?></h1>
app/design/frontend/Vendor/Custom_Module/Magento_Catalog/layout/catalog_product_view.xml
<referenceContainer name="content">
<block
class="Magento\Framework\View\Element\Template"
name="pop_up"
template="Custom_Module::templates/product.phtml"/>
</referenceContainer>