3

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".

  • Have you managed to get this sorted out? we are trying to do a similar thing. – user2844 Aug 5 '13 at 11:37
  • @Branden: Please consider upvoting ... – Alex Aug 5 '13 at 12:35
1

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.

0

Came across this and will add my suggestion as it worked for me. Unfortunately, the javascript doesn't work properly (though that may be a localized issue for me).

In my case, I have a bundled product where the associated products have custom options. This isn't supported by Magento (hence my work in this area). On the front-end, only the associated product shows as an option, but the associated product's custom options do not.

Let's see a code example:

// Get the product and the product's options - In this case, the options are associated products attached to a bundled parent product
$product = Mage::getModel('catalog/product')->load($this->getProduct()->getId());
$productOptionIds = $product->getTypeInstance(true)->getChildrenIds($product->getId());

foreach ($productOptionIds as $optionId) {
    // The option ID is the ID of the product, let's load that associated product
    $associatedProduct = Mage::getModel('catalog/product')->load($optionId);
    // Now lets get the custom options assigned to the associated product
    foreach ($associatedProduct->getProductOptionsCollection() as $option) {

        // Now we get the block in the layout responsible for rendering, you'll see this declared in catalog.xml most likely.
        $block = Mage::app()->getLayout()->getBlock('product.info.options');
        // Assuming the type of option we're using has a renderer added, we can then just get the HTML
        echo $block->getOptionHtml($option);
    }
}

Hopefully that helps some folks, as I was not able to find much info about this. Also, using this as I've presented will likely mean that the price updating on the product page will not work properly. I leave this code here as an example only, the implementation will be up to you.

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.