Display HTML code (block) of product options in Magento

Display HTML code (block) of product options in Magento

This simple helper will help you to display html code of your product options. It works with simple, virtual and configurable products. I believe you can use the same approach for other product types as well.

Getting HTML code for options of configurable products its little different from simple and virtual (well they are more or less same).

Update: for all types in configurable product.


With simple products we have a very “clean approach”.

First we are setting a block for product options, then we add item renderers (template and type) to product options block. And the last step is to take the product, create iteration over options and display it with our product options block.

We are seeing here some nice loose coupling. Nice separation of code.

On the other hand, getting html code of options in configurable product is not that flexible.

Instance of block, product instance and we are getting html for options.

Sample:

echo Mage::helper("inchoo_checkout")->getProductOptionsHtml(Mage::getModel("catalog/product")->load(171));

Helper:

<!--?php <br ?-->/**
 * @category    Inchoo
 * @package     Inchoo_Checkout
 * @author      Inchoo
 */
class Inchoo_Checkout_Helper_Data extends Mage_Core_Helper_Abstract
{
	/**
	 * 
	 * getting html for options of products
	 * @param Mage_Catalog_Model_Product $product
	 */
	public function getProductOptionsHtml(Mage_Catalog_Model_Product $product)
	{ 
		$blockOption = Mage::app()->getLayout()->createBlock("Mage_Catalog_Block_Product_View_Options");
        $blockOption->addOptionRenderer("default","catalog/product_view_options_type_default","catalog/product/view/options/type/default.phtml");
		$blockOption->addOptionRenderer("text","catalog/product_view_options_type_text","inchoo_catalog/product/view/options/type/text.phtml");
		$blockOption->addOptionRenderer("file","catalog/product_view_options_type_file","catalog/product/view/options/type/file.phtml");
		$blockOption->addOptionRenderer("select","inchoo_checkout/product_view_options_type_select","catalog/product/view/options/type/select.phtml");
		$blockOption->addOptionRenderer("date","catalog/product_view_options_type_date","catalog/product/view/options/type/date.phtml") ;
 
 
		$blockOptionsHtml = null;
 
		 if($product->getTypeId()=="simple"||$product->getTypeId()=="virtual"||$product->getTypeId()=="configurable")
		 {  
		 	$blockOption->setProduct($product);
			if($product->getOptions())
			{  
				foreach ($product->getOptions() as $o) 
	    		{     
	    			$blockOptionsHtml .= $blockOption->getOptionHtml($o); 
	    		};    
			}  
		 }  
 
		 if($product->getTypeId()=="configurable")
		 {   
		 	$blockViewType = Mage::app()->getLayout()->createBlock("Mage_Catalog_Block_Product_View_Type_Configurable");
		 	$blockViewType->setProduct($product);   
		 	$blockViewType->setTemplate("inchoo_catalog/product/view/type/options/configurable.phtml");
			$blockOptionsHtml .= $blockViewType->toHtml(); 
		 }  
		 return $blockOptionsHtml; 
	}
}

You made it all the way down here so you must have enjoyed this post! You may also like:

Wrap WYSIWYG content using custom HTML Danijel Vrgoc
, | 0

Wrap WYSIWYG content using custom HTML

Sorry, we can’t ship there Sasa Brankovic
Sasa Brankovic, | 12

Sorry, we can’t ship there

Shell script for converting configurable to grouped products Tsvetan Stoychev
Tsvetan Stoychev, | 5

Shell script for converting configurable to grouped products

9 comments

  1. Fatal error: Call to a member function setTemplate() on boolean in D:\xampp\htdocs\magento\app\code\core\Mage\Catalog\Block\Product\View\Options.php on line 197
    i got this error while running the above code how can i solve this?

    1. Renderers are not corrects try this :

      $blockOption->addOptionRenderer(“default”,”catalog/product_view_options_type_default”,”catalog/product/view/options/type/default.phtml”);
      $blockOption->addOptionRenderer(“text”,”catalog/product_view_options_type_text”,”catalog/product/view/options/type/text.phtml”);
      $blockOption->addOptionRenderer(“file”,”catalog/product_view_options_type_file”,”catalog/product/view/options/type/file.phtml”);
      $blockOption->addOptionRenderer(“select”,”catalog/product_view_options_type_select”,”catalog/product/view/options/type/select.phtml”);
      $blockOption->addOptionRenderer(“date”,”catalog/product_view_options_type_date”,”catalog/product/view/options/type/date.phtml”);

    1. Hi there, have you managed to find an answer for your question? I’d be interested in knowing it too. Thanks! 🙂

  2. Hi,

    thank you very much for this! i had to add

    $blockViewType->setChild('attr_renderers', $blockOption);
    $blockOptionsHtml .= $blockViewType->toHtml();

    for it to work. cheers

  3. i create custom phtml file all porduct option working but file upload not working i have search every where but find solution plz help

    on porduct they working fine

  4. If you were to do this via xml it would be crazy so having this in a helper is so useful!

    Thank you very much.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.