Load price phtml file in magento2

we have to get product price HTML using Magento 2 below way,

we have to create a block Vendor\Module\Block\Product. PHP and get your product object and call the below function in your template file.

protected $resultLayout;

public function __construct(
\Magento\Framework\View\Result\Layout $resultLayout
) {
$this->resultLayout = $resultLayout;
}

public function getProductPriceHtml(\Magento\Catalog\Model\Product $product)
{
/** @var \Magento\Framework\Pricing\Render $priceRender */
$priceRender = $this->resultLayout->getLayout()->getBlock(\Magento\Framework\Pricing\Render::class);
if (!$priceRender) {
$priceRender = $this->resultLayout->getLayout()->createBlock(
\Magento\Framework\Pricing\Render::class,
\Magento\Framework\Pricing\Render::class,
[‘data’ => [‘price_render_handle’ => ‘catalog_product_prices’]]
);
}
$price = ”;
if ($priceRender) {
$price = $priceRender->render(
\Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE,
$product,
[
‘display_minimal_price’ => true,
‘use_link_for_as_low_as’ => true,
‘zone’ => \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST,
]
);
}

return $price;

}

Leave a comment

Your email address will not be published. Required fields are marked *