The question:
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 VendorHelloworldBlock;
class Helloworld extends MagentoFrameworkViewElementTemplate {
/** @var MagentoFrameworkViewResultPage */
protected $_product;
protected $_layout;
public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
MagentoCatalogModelProduct $product,
MagentoFrameworkViewLayoutInterface $layout
) {
$this->_product = $product;
$this->_layout = $layout;
parent::__construct($context);
}
public function getPrice($productID) {
$product = $this->_product->load($productID);
$price = '';
$arguments = [];
$priceType = MagentoCatalogPricingPriceFinalPrice::PRICE_CODE;
$priceRender = $this->getLayout()->getBlock('MagentoFrameworkPricingRender', 'product.price.render.default');
$arguments['zone'] = MagentoFrameworkPricingRender::ZONE_ITEM_LIST;
if ($priceRender) {
$price = $priceRender->render($priceType, $product, $arguments);
}
return $price;
}
}
Controller File :
<?php
namespace VendorHelloworldControllerIndex;
class Custom extends MagentoFrameworkAppActionAction {
protected $_customBlock;
public function __construct(
MagentoFrameworkAppActionContext $context,
VendorHelloworldBlockHelloworld $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="VendorHelloworldBlockHelloworld" name="formbuilder" template="Vendor_Helloworld::helloworld.phtml"/>
</referenceContainer>
</body>
</page>
I take reference from this both answer :
1) Click here
2) Click here
Thanks.
The Solutions:
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Method 1
For render price template in the specific block,
you need to call function like below,
public function getPrice(MagentoCatalogModelProduct $product)
{
$priceRender = $this->getLayout()->getBlock('product.price.render.default');
if (!$priceRender) {
$priceRender = $this->getLayout()->createBlock(
MagentoFrameworkPricingRender::class,
'product.price.render.default',
['data' => ['price_render_handle' => 'catalog_product_prices']]
);
}
$price = '';
if ($priceRender) {
$price = $priceRender->render(
MagentoCatalogPricingPriceFinalPrice::PRICE_CODE,
$product,
[
'display_minimal_price' => true,
'use_link_for_as_low_as' => true,
'zone' => MagentoFrameworkPricingRender::ZONE_ITEM_LIST
]
);
}
return $price;
}
Method 2
You can try below solution:
$arguments = [];
$priceType = MagentoCatalogPricingPriceFinalPrice::PRICE_CODE;
$priceRender = $this->getLayout()->getBlock('product.price.render.default');
$arguments['zone'] = MagentoFrameworkPricingRender::ZONE_ITEM_LIST;
$price = ''
if ($priceRender) {
$price = $priceRender->render($priceType, $product, $arguments);
}
echo $price;
If layout still not render, then you need to inject MagentoFrameworkAppViewInterface
in your construct :
/**
* @var MagentoFrameworkAppViewInterface|PHPUnit_Framework_MockObject_MockObject
*/
protected $_view;
public function __construct(
..........
MagentoFrameworkAppViewInterface $view,
..........
) {
..........
$this->_view = $view;
..........
}
And need to inject MagentoFrameworkViewResultPageFactory
in your controller
/**
* @var MagentoFrameworkViewResultPageFactory
*/
protected $_resultPageFactory;
public function __construct(
..........
MagentoFrameworkViewResultPageFactory $resultPageFactory,
..........
) {
..........
$this->_resultPageFactory = $resultPageFactory;
..........
}
public function execute() {
........
$resultPage = $this->_resultPageFactory->create();
........
}
Then, you can use this below line for successfully price render layout.
$priceRender = $this->getLayout()->getBlock('product.price.render.default');
Method 3
Try This Code
protected $resultLayout;
public function __construct(
MagentoFrameworkViewResultLayout $resultLayout
) {
$this->resultLayout = $resultLayout;
}
public function getProductPriceHtml(MagentoCatalogModelProduct $product)
{
/** @var MagentoFrameworkPricingRender $priceRender */
$priceRender = $this->resultLayout->getLayout()->getBlock(MagentoFrameworkPricingRender::class);
if (!$priceRender) {
$priceRender = $this->resultLayout->getLayout()->createBlock(
MagentoFrameworkPricingRender::class,
MagentoFrameworkPricingRender::class,
['data' => ['price_render_handle' => 'catalog_product_prices']]
);
}
$price = '';
if ($priceRender) {
$price = $priceRender->render(
MagentoCatalogPricingPriceFinalPrice::PRICE_CODE,
$product,
[
'display_minimal_price' => true,
'use_link_for_as_low_as' => true,
'zone' => MagentoFrameworkPricingRender::ZONE_ITEM_LIST,
]
);
}
return $price;
}
I Hope This Helps You.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0