Question

In magento 2.3 i want bundle item options as per in product detail page.How can i get all html of bundle item. Please suggest any solution

Was it helpful?

Solution

Try this, it will work 100%.

 public function getBundleOptionsHtml($product)
 {
    $_product = $this->_objectManager->create('\Magento\Catalog\Model\Product')->load($product->getId());

    $this->coreRegistry->register('current_product',$_product);/*this used for default file of observer*/
    $layout = $this->layoutFactory->create();
    $blockOption = $layout->createBlock('Magento\Bundle\Block\Catalog\Product\View\Type\Bundle', 'type_bundle_options')->setProduct($_product)->setTemplate('Magento_Bundle::catalog/product/view/type/bundle/options.phtml');
    $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');

    $blockOption->setChild('product.price.render.default', $price_renderer_block);


    $block_links2 = $layout->createBlock('Magento\Bundle\Block\Catalog\Product\View\Type\Bundle\Option\Multi', 'multi')->setProduct($_product);
    $blockOption->setChild('multi', $block_links2);

    $block_links3 = $layout->createBlock('Magento\Bundle\Block\Catalog\Product\View\Type\Bundle\Option\Radio', 'radio')->setProduct($_product);
    $blockOption->setChild('radio', $block_links3);


    $block_links4 = $layout->createBlock('Magento\Bundle\Block\Catalog\Product\View\Type\Bundle\Option\Select', 'select')->setProduct($_product);
    $blockOption->setChild('select', $block_links4);


    $block_links5 = $layout->createBlock('Magento\Bundle\Block\Catalog\Product\View\Type\Bundle\Option\Checkbox', 'checkbox')->setProduct($_product);
    $blockOption->setChild('checkbox', $block_links5);


    return $blockOption->toHtml();

}

Call this function where you want to get html of bundle product by pass product in argument.

OTHER TIPS

Please check with below code :

   $store_id = $_storeManager->getStore()->getId();
                $options = $_objectManager->get('Magento\Bundle\Model\Option')
             ->getResourceCollection()
                              ->setProductIdFilter($product->getId())
                              ->setPositionOrder();
             $options->joinValues($store_id);
    $typeInstance = $_objectManager->get('Magento\Bundle\Model\Product\Type');
    $selections = $typeInstance->getSelectionsCollection($typeInstance->getOptionsIds($product), $product);

I hope its work for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top