2

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

asked Jul 5, 2017 at 13:44
2
  • Did you found any solution for magento 2 ? Commented Sep 19, 2018 at 12:32
  • Can you please help me to solve this? It's very urgent for me. Commented Oct 12, 2018 at 7:14

1 Answer 1

4

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;
 }
answered Aug 6, 2019 at 10:06
3
  • 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? Commented Feb 17, 2020 at 6:43
  • @ManishMaheshwari, above code is not working in Magento 2.3.4. Can you help? Commented May 26, 2020 at 12:01
  • The code is working on 2.4.2! Commented May 23, 2021 at 11:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.