Magento – Magento dropdown attribute default value

configurable-productproduct-attribute

As I found the option box default value in magento here:

/app/design/frontend/default/mytheme/template/catalog/product/view/type/options/configurable.phtml

<select name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select">
<option><?php echo $this->__('Choose an Option...') ?></option>
</select>

It shows 'Choose an Option…' as a default value

I try

<script>
 $(function() {
 $("#attribute525 option:first-child").attr("selected", true); //color
 $("#attribute272 option:first-child").attr("selected", true); //size
});
</script>

to make the default value each attribute set to the first option of each attribute,

but it doesn't work. Any suggestion will be appreciated, thanks.

Best Answer

This is how I do it. In

/app/design/frontend/default/mytheme/template/catalog/product/view/type/options/configurable.phtml

Add this

    Product.Config.prototype.checkOptions = function(element) {
        var numOptions = element.options.length-1;
        if(numOptions > 1) {
            element.up('dd').show().previous().show();
        }
        if(numOptions === 1) {
            element.options[1].selected = true;
            this.configureElement(element);
        }
    };
    Product.Config.prototype.initialize = (function(_super) {
        return function() {
            _super.apply(this, arguments);
            this.settings.each(function(element) {
                element.up('dd').hide().previous().hide();
            });
            this.checkOptions(this.settings.first());
        };
    })(Product.Config.prototype.initialize);
    Product.Config.prototype.configureElement = (function(_super) {
        return function() {
            _super.apply(this, arguments);
            if (!arguments[0].nextSetting) {
                return;
            }
            this.checkOptions(arguments[0].nextSetting);
        };
    })(Product.Config.prototype.configureElement);

above

var spConfig = new Product.Config(<?php echo $_jsonConfig ?>);

This also adds show/hide but you can remove those lines if you prefer.