I want to render specific product price render in my custom page in block file.
I get reference from here . But, It returns error :
main.CRITICAL: Wrong Price Rendering layout configuration. Factory block is missed [] []
How to render price box ?
Please help me.
Block File :
<?php
namespace Vendor\Helloworld\Block;
class Helloworld extends \Magento\Framework\View\Element\Template {
/** @var \Magento\Framework\View\Result\Page */
protected $_product;
protected $_layout;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Model\Product $product,
\Magento\Framework\View\LayoutInterface $layout
) {
$this->_product = $product;
$this->_layout = $layout;
parent::__construct($context);
}
public function getPrice($productID) {
$product = $this->_product->load($productID);
$price = '';
$arguments = [];
$priceType = \Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE;
$priceRender = $this->getLayout()->getBlock('Magento\Framework\Pricing\Render', 'product.price.render.default');
$arguments['zone'] = \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST;
if ($priceRender) {
$price = $priceRender->render($priceType, $product, $arguments);
}
return $price;
}
}
Controller File :
<?php
namespace Vendor\Helloworld\Controller\Index;
class Custom extends \Magento\Framework\App\Action\Action {
protected $_customBlock;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Vendor\Helloworld\Block\Helloworld $customBlock
) {
$this->_customBlock = $customBlock;
parent::__construct($context);
}
public function execute() {
return $this->_customBlock->getPrice(1);
}
}
layout xml : (helloworld_index_index.xml)
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Vendor\Helloworld\Block\Helloworld" name="formbuilder" template="Vendor_Helloworld::helloworld.phtml"/>
</referenceContainer>
</body>
</page>
I take reference from this both answer :
1) Click here 2) Click here
Thanks.