2

How do i set my radio button custom option to a default value? Now nothing is selected by default.

I suspect I can add or change something in -->

Mage/Catalog/Block/Product/View/Options/Type/Select.php?

1

2 Answers 2

5

Yes you are correct, the custom options are rendered from the file app/code/core/Mage/Catalog/Block/Product/View/Options/Type/Select.php

To set a set a radio button custom option to a default value, write this code

if($_value->getTitle() == 'Title_of_your_custom_option')
{
    $checked = 'checked';
}

before the below code

$selectHtml .= '<li>' . '<input type="' . $type . '" class="' . $class . ' ' . $require
                    . ' product-custom-option"'
                    . ($this->getSkipJsReloadPrice() ? '' : ' onclick="opConfig.reloadPrice()"')
                    . ' name="options[' . $_option->getId() . ']' . $arraySign . '" id="options_' . $_option->getId()
                    . '_' . $count . '" value="' . $htmlValue . '" ' . $checked . ' price="'
                    . $this->helper('core')->currencyByStore($_value->getPrice(true), $store, false) . '" />'
                    . '<span class="label"><label for="options_' . $_option->getId() . '_' . $count . '">'
                    . $_value->getTitle() . ' ' . $priceStr . '</label></span>';
4
  • I got it working, but it seems to add a (new) third option which is then checked by default. So, almost there, but I cannot see what I'm doing wrong. Jul 8, 2015 at 11:45
  • post your code here or else try my solution, It works perfectly for me and it wont add a new option Jul 8, 2015 at 11:46
  • Not sure how to post that much code, but you can download the file from here (just change .zip to php or txt) link Jul 8, 2015 at 11:55
  • You added the code at the wrong place. look at my answer. i have specified before which $selectHtml to add the code Jul 8, 2015 at 12:11
0

If you have a place to put some JavaScript, you could also check the first radio button in all of these option lists:

$('.options-list').find('input[type=radio]').first().prop('checked','checked');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.