Magento utility function: How to easily create a select box for drop-down attributes?

From now onward I will be sharing custom utility functions on a regular basis which will make your programming life easier to some extent at least in case of Magento 🙂

Here I am going to share a utility function which will help you to create a select box for drop-down attributes(manufacturer, color, size, etc.) of a product.

Utility Function:

function getSelectBox($attributeCode, $label = '', $defaultSelect = null, $extraParams = null){
	$options			= array();
	$product			= Mage::getModel('catalog/product');
	$attribute			= $product->getResource()->getAttribute($attributeCode);
	if($attribute->usesSource()){
		$options = $attribute->getSource()->getAllOptions(false);
		array_unshift($options, array('label' => $label, 'value' => ''));
	}

	$select = Mage::app()->getLayout()->createBlock('core/html_select')
			->setName($attributeCode)
			->setId($attributeCode)
			->setTitle($label)
			->setValue($defaultSelect)
			->setExtraParams($extraParams)
			->setOptions($options);
	return $select->getHtml();
}

Now you can easily draw a select box for any drop-down attribute (for example manufacturer) by simply calling the above function as:

<?php echo getSelectBox('manufacturer', 'Select Manufacturer'); ?>

Output:

Select Box - Manufacturer
Select Box – Manufacturer

Note: You can also pass a default value to be selected, extra parameter like CSS class, javascript event handlers, etc via function arguments. Or you can customize the function easily as per your requirements.

You can define the above function in your Helper class and can call from anywhere within your application in order to create a select box for any drop-down attributes of the product.

Happy Coding!!

13 thoughts on “Magento utility function: How to easily create a select box for drop-down attributes?”

  1. This is some nice code.
    It would be great if you could elaborate for a lot of people and give a beginners guide walk through the process of creating say a magento side block for the homepage with manufacturers dropdown.
    Many people are switching to magento from other platforms or new to ecommerce altogether and this sort of instruction would be very useful.

    Keep up the good work!

    Reply
  2. Hello,

    I want to change the “dropdown ” into checkboxes or radio buttons. I have a checkout module that saves the address for logged in customers. In checkout page it will appear a dropdown with the saved address, but i need a checkbox. Where I should modify the code ?

    Reply
  3. Dear MagePsycho,

    Can you please tell me how to add custom dropdown field in Customer Registration Page in Magento?
    I searched a lot but the thing is every where it is given for textbox. One where I found is http://www.excellencemagentoblog.com/customer-registration-fields-magento1-6 but in this STEP2 Source Model it is given. Now the thing is for that code the file is not given that where to put this code. I have even no custom module for it. That code is given for a custom module.

    Can you please help?

    Thanks
    Kaushal

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.