Magento – Render bundle product options in block

bundlerender

I'm trying to render a bundle product's options (html) in a block for a custom module that I'm working on, without much success and I hit a dead end so far.

For for configurable or grouped products it works with:

$blockOptionsHtml = null;
if($product->getTypeId()=="configurable")
{
    $blockViewType = Mage::app()->getLayout()
        ->createBlock("Mage_Catalog_Block_Product_View_Type_Configurable");
    $blockViewType->setProduct($product);
    $blockViewType
        ->setTemplate("catalog/product/view/type/options/configurable.phtml");
    $blockOptionsHtml .= $blockViewType->toHtml();
}
return $blockOptionsHtml();

The same is not working for a bundle product.

Well, we modified the type in createBlock type and the addOptionRenderer, which for bundles is actually just addRenderer and doesn't accept the 3rd parameter (the template, which is declared inside the block file).

Hope this makes any sense and maybe someone has an idea, it will be greately appreciated, as I'm pretty stuck for the moment…

Code we use for bundles:

$blockOptionsHtml = null;
$blockViewType = Mage::app()->getLayout()
    ->createBlock("Mage_Bundle_Block_Catalog_Product_View_Type_Bundle_Option");
$blockViewType->setProduct($product);
$blockViewType->setTemplate("bundle/catalog/product/view/type/bundle/options.phtml");
$blockOptionsHtml .= $blockViewType->toHtml();

but in the browser the message is "No options of this product are available".

Best Answer

Some "Help to help yourself:"

Do you have to possibility to debug this in Xdebug? Then you should set a breakpoint around your line

$blockOptionsHtml .= $blockViewType->toHtml();

Actually - as you setting this null before, a simple

$blockOptionsHtml = $blockViewType->toHtml(); also does.

If you do not have a debugger (even I highly suggest to install one), you could also use poor-man's debugging like adding a

die('That Line in ' . __FILE__) was executed and see if this text is shown in the browser.

The next step is to go through bundle/catalog/product/view/type/bundle/options.phtml line by line and see what is going on. Maybe this can already give you a hint what's going on.

General point:

  • Please create blocks via the short code (not the full class name)

My guess is , that in the options.phtml Magento can not properly access the product or same data is missing. But it is best to find out this with the debugger (or some echo & die debug code). Then post a comment, in which line something is missing.

Related Topic