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

  • Did you found any solution for magento 2 ? – Rohan Hapani Sep 19 '18 at 12:32
  • Can you please help me to solve this? It's very urgent for me. – Rohan Hapani Oct 12 '18 at 7:14
2

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')->load($product);

            $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;
            }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.