Display HTML code (block) of product options in Magento

Featured Image

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;
	}
}
2
Top

Care to rate this post?

Author

Goran Sambolic

Ex Inchooer

Goran worked at Inchoo from 2011 to 2012

Other posts from this author

Discussion 2 Comments

Add Comment
  1. Rees

    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.

  2. arshad

    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

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top