June 21, 2011

Magento is a great tool, but sometimes adds some unnecessary friction to the buying process. Take Custom Product Options drop-down list for example. There is no way to set an option as a default.

If you have several custom options on a product, but 80% of the customers are buying a certain option there is no point in making those customers actually choose the option from the list – it should be set by default.

There is a relatively easy fix for this one.

CAUTION::To do this requires editing of one of the Core Magento files. If you upgrade your Magento version, it will overwrite this code. Also, this will apply to all Product Custom Option Drop-down lists on your site.

This example is based on Magento 1.4.2.0. so the exact location in the file may be different if you are running a different version.

First make sure you set the most popular option to the first in the sort order on your Custom Options tab. Now you want to fine the file app/code/Mage/Catalog/Block/Product/View/Options/Type/Select.php. Look for the following code (in 1.4.2.0 it starts on line 52):

if ($_option->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_DROP_DOWN) {

$select->setName('options['.$_option->getid().']')

->addOption('', $this->__('-- Please Select --'));

You’ll want to edit out the line that calls for the addOption():

if ($_option->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_DROP_DOWN) {

$select->setName('options['.$_option->getid().']') ; //<- don't miss this semi-colon

// ->addOption('', $this->__('-- Please Select --'));

That’s it! Now refresh your browser and your Custom Option Drop-down should default to the first option in the sort order.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • LinkedIn
  • Technorati
  • Slashdot
  • Yahoo! Buzz
  • Google Bookmarks
  • Ping.fm
  • Reddit
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Twitter

2 Responses to “Magento Custom Product Option Default Value”

  1. Chris Harding Says:

    Hi

    Thanks for the post, how do you do it for radio buttons as well as select lists?

    Regards

    Chris

  2. Geoff Kliza Says:

    Hi Chris,

    Magento puts a “None” there for non-required options since there is no way to de-select a radio button. Either way if you want to make the first option selected, you would edit the same file. Look around line 87 for the following:

    if (!$_option->getIsRequire()) {
    $selectHtml .= '<li><input type="radio" id="options_'.$_option->getId().'" class="'.$class.' product-custom-option" name="options['.$_option->getId().']" onclick="opConfig.reloadPrice()" value="" checked="checked" /><span class="label"><label for="options_'.$_option->getId().'">'. $this->__('None') . '</label></span></li>';
    }

    Comment this entire piece out. It adds the “None” option to the radio buttons and sets it as selected.

    Next find the foreach loop just below it that starts with:

    $count = 1;
    foreach ($_option->getValues() as $_value) {

    In that loop, find:

    $selectHtml .= '<li>' .
    '<input type="'.$type.'" class="'.$class.' '.$require.' product-custom-option" onclick="opConfig.reloadPrice()" name="options['.$_option->getId().']'.$arraySign.'" id="options_'.$_option->getId().'_'.$count.'" value="'.$_value->getOptionTypeId().'" />' .
    '<span class="label">

    and change to:

    $checked = NULL;
    if($count == 2) { $checked = ' checked="checked"'; }

    $selectHtml .= '<li>' .
    '<input type="'.$type.'" class="'.$class.' '.$require.' product-custom-option" onclick="opConfig.reloadPrice()" name="options['.$_option->getId().']'.$arraySign.'" id="options_'.$_option->getId().'_'.$count.'" value="'.$_value->getOptionTypeId().'"'.$checked.' />' .
    '<span class="label"><label for="options_'.$_option->getId().'_'.$count.'">'.$_value->getTitle().' '.$priceStr.'</label></span>';

    Note the addition of the $checked to the end of the <input> tag. Be very careful about the single and double quote arrangement at the end if you are re-typing this.

    That will work for both Radio and Check-boxes.


Leave a Reply