Create layout catalog_product_view.xml :
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="product.info.main">
<block class="Vendor\Module\Block\View\Test" name="test" template="Vendor_Module::account/test.phtml"/>
</referenceContainer>
</body>
</page>
Create block and extend it with view like below :
<?php
declare(strict_types=1);
namespace Vendor\Module\Block\View;
use Magento\Catalog\Block\Product\View;
use Magento\Catalog\Api\ProductRepositoryInterface;
class Test extends View
{
/**
* @param \Magento\Catalog\Block\Product\Context $context
* @param \Magento\Framework\Url\EncoderInterface $urlEncoder
* @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
* @param \Magento\Framework\Stdlib\StringUtils $string
* @param \Magento\Catalog\Helper\Product $productHelper
* @param \Magento\Catalog\Model\ProductTypes\ConfigInterface $productTypeConfig
* @param \Magento\Framework\Locale\FormatInterface $localeFormat
* @param \Magento\Customer\Model\Session $customerSession
* @param ProductRepositoryInterface $productRepository
* @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
* @param array $data
*/
public function __construct(
\Magento\Catalog\Block\Product\Context $context,
\Magento\Framework\Url\EncoderInterface $urlEncoder,
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
\Magento\Framework\Stdlib\StringUtils $string,
\Magento\Catalog\Helper\Product $productHelper,
\Magento\Catalog\Model\ProductTypes\ConfigInterface $productTypeConfig,
\Magento\Framework\Locale\FormatInterface $localeFormat,
\Magento\Customer\Model\Session $customerSession,
ProductRepositoryInterface $productRepository,
\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
array $data = []
)
{
parent::__construct($context, $urlEncoder, $jsonEncoder, $string, $productHelper, $productTypeConfig, $localeFormat, $customerSession, $productRepository, $priceCurrency, $data);
}
/**
* @return array
*/
public function getProductData()
{
$product = $this->getProduct();
if($product) {
return $product;
}
return [];
}
}
And call this function in phtml using $block like below.
create test.phtml :
<?php
/** @var Vendor\Module\Block\View\Test $block */
$productData = $block->getProductData();
$productSku = $productData->getSku();