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:

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!!
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!
Where exactly does this code need to go? I want the top default option in the dropdown to “select all”.
Try as:
echo getSelectBox('attribute-code-goes-here', 'Select All');Thanks
I was wondering if you have done this for a text field attribute.
Hello, how can I do to create a dropdown list with the CMS pages?
Thank you !
i need to display categories by selected manufacutrer id , is it possible?
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 ?
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
i can’t seem to get this working – where does the utility function code go?
thanks in advance
where to put this code in magento ? please elaborate…
how can i load prodect list from selected attribute option ? thanks in advance