I am trying to render custom option in my custom page. I found an example for magento 1 here but it dint work out as in magento 2 addOptionRenderer is removed I guess.
I tried this to call in from ajax which will return html
$layout = $this->layoutFactory->create();
$block_header = $layout->createBlock('Magento\Catalog\Block\Product\View\Options')
->setTemplate('Magento_Catalog::product/view/options.phtml');
$block_links1 = $layout->createBlock('Magento\Catalog\Block\Product\View\Options\Type\DefaultType','default')->setTemplate('Magento_Catalog::product/view/options/type/default.phtml');
$block_header->setChild('default',$block_links1);
$block_links2 = $layout->createBlock('Magento\Catalog\Block\Product\View\Options\Type\Text','text')->setTemplate('Magento_Catalog::product/view/options/type/text.phtml');
$block_header->setChild('text',$block_links2);
$block_links3 = $layout->createBlock('Magento\Catalog\Block\Product\View\Options\Type\File','file')->setTemplate('Magento_Catalog::product/view/options/type/file.phtml');
$block_header->setChild('file',$block_links3);
$block_links4 = $layout->createBlock('Magento\Catalog\Block\Product\View\Options\Type\Select','select')->setTemplate('Magento_Catalog::product/view/options/type/select.phtml');
$block_header->setChild('select',$block_links4);
$block_links5 = $layout->createBlock('Magento\Catalog\Block\Product\View\Options\Type\Date','date')->setTemplate('Magento_Catalog::product/view/options/type/date.phtml');
$block_header->setChild('date',$block_links5);
echo $block_header->toHtml();
The error I am getting is
Fatal error: Uncaught Error: Call to a member function renderAmount() on boolean in /my/magento/path/vendor/magento/module-catalog/Block/Product/View/Options/AbstractOptions.php on line 143
-
Did you found any solution for magento 2 ?Rohan Hapani– Rohan Hapani2018年09月19日 12:32:51 +00:00Commented Sep 19, 2018 at 12:32
-
Can you please help me to solve this? It's very urgent for me.Rohan Hapani– Rohan Hapani2018年10月12日 07:14:51 +00:00Commented Oct 12, 2018 at 7:14
1 Answer 1
I have did code in magento 2.3.0 and its working for me
$_product = $this->_objectManager->create('\Magento\Catalog\Model\Product')->load($product);
$layout = $this->_objectManager->create('\Magento\Framework\View\LayoutFactory');
$blockOptionData = $layout->createBlock("Magento\Catalog\Block\Product\View\Options")->setProduct($_product)
->setTemplate('Magento_Catalog::product/view/options.phtml');
$block_links1 = $layout->createBlock('Magento\Catalog\Block\Product\View\Options\Type\DefaultType', 'default')->setTemplate('Magento_Catalog::product/view/options/type/default.phtml');
$blockOptionData->setChild('default', $block_links1);
$block_links2 = $layout->createBlock('Magento\Catalog\Block\Product\View\Options\Type\Text', 'text')->setTemplate('Magento_Catalog::product/view/options/type/text.phtml');
$blockOptionData->setChild('text', $block_links2);
$block_links3 = $layout->createBlock('Magento\Catalog\Block\Product\View\Options\Type\File', 'file')->setTemplate('Magento_Catalog::product/view/options/type/file.phtml');
$blockOptionData->setChild('file', $block_links3);
$block_links4 = $layout->createBlock('Magento\Catalog\Block\Product\View\Options\Type\Select', 'select')->setTemplate('Magento_Catalog::product/view/options/type/select.phtml');
$blockOptionData->setChild('select', $block_links4);
$block_links5 = $layout->createBlock('Magento\Catalog\Block\Product\View\Options\Type\Date', 'date')->setTemplate('Magento_Catalog::product/view/options/type/date.phtml');
$blockOptionData->setChild('date', $block_links5);
$option_price_renderer_block = $layout
->createBlock(
"Magento\Framework\Pricing\Render", "product.price.render.default", [
'data' => [
'price_render_handle' => 'catalog_product_prices',
'use_link_for_as_low_as' => 'true'
]
]
)
->setData('area', 'frontend');
$blockOptionData->setChild('product.price.render.default', $option_price_renderer_block);
$blockOptionData->setProduct($_product);
if (!empty($blockOptionData->toHtml()) && strlen($blockOptionData->toHtml()) > 1) {
return $blockOptionData->toHtml();
} else {
return false;
}
-
I am getting error in Magento_Catalog::product/view/options.phtml file. <script type="text/x-magento-init"> { "#product_addtocart_form": { "priceOptions": { "optionConfig": <br /> <b>Fatal error</b>: Uncaught Error: Call to a member function getTypeId() on null in /opt/lampp/htdocs/libidexm2/vendor/magento/module-tax/Observer/GetPriceConfigurationObserver.php:50 Can you help in this?Gaurav Khatri– Gaurav Khatri2020年02月17日 06:43:01 +00:00Commented Feb 17, 2020 at 6:43
-
@ManishMaheshwari, above code is not working in Magento 2.3.4. Can you help?Payal Patel– Payal Patel2020年05月26日 12:01:20 +00:00Commented May 26, 2020 at 12:01
-
The code is working on 2.4.2!Abdul Samad Abbasi– Abdul Samad Abbasi2021年05月23日 11:36:03 +00:00Commented May 23, 2021 at 11:36