4

How to set first value attribute as selected for a configurable product?

Front-end:

Size:

  • Choose an Option... (selected)

  • Small

  • Medium

I would like to change it to...

Size:

  • Small (selected)
  • Medium

configurable.phtml

<?php
$_product    = $this->getProduct();
$_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes());
$_jsonConfig = $this->getJsonConfig();
$_renderers = $this->getChild('attr_renderers')->getSortedChildren();
?>
<?php if ($_product->isSaleable() && count($_attributes)):?>
    <dl>
    <?php foreach($_attributes as $_attribute): ?>
        <?php
        $_rendered = false;
        foreach ($_renderers as $_rendererName):
            $_renderer = $this->getChild('attr_renderers')->getChild($_rendererName);
            if (method_exists($_renderer, 'shouldRender') && $_renderer->shouldRender($_product, $_attribute, $_jsonConfig)):
                $_renderer->setProduct($_product);
                $_renderer->setAttributeObj($_attribute);
                echo $_renderer->toHtml();
                $_rendered = true;
                break;
            endif;
        endforeach;

        if (!$_rendered):
        ?>
        <dt><label class="required"><em>*</em><?php echo $_attribute->getLabel() ?></label></dt>
        <dd<?php if ($_attribute->decoratedIsLast){?> class="last"<?php }?>>
            <div class="input-box">
                <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>
              </div>
        </dd>
        <?php endif; ?>
    <?php endforeach; ?>
    </dl>
<?php
$jsonConfig = json_decode($this->getJsonConfig());
$jsonConfig->chooseText = $_attribute->getLabel();
?>

<script type="text/javascript">
    var spConfig = new Product.Config(<?php echo json_encode($jsonConfig); ?>);
</script>
    <?php echo $this->getChildHtml('after') ?>
<?php endif;?>
0

1 Answer 1

1

goto /app/design/frontend/your_package/default/template/catalog/product/view/type/options and openconfigurable.phtml

find for this code

<script type="text/javascript">
    var spConfig = new Product.Config(<?php echo $this->getJsonConfig() ?>);
</script>

and replace with

<?php
$jsonConfig = json_decode($this->getJsonConfig());
$jsonConfig->chooseText = $_attribute->getLabel();
?>

<script type="text/javascript">
    var spConfig = new Product.Config(<?php echo json_encode($jsonConfig); ?>);
</script>
    <?php echo $this->getChildHtml('after') ?>
<?php endif;?>
2
  • 1
    Yes it won't give first attribute value but attribute label you should fetch attributes values and then display them before posting this answer i tried for that but I failed so atleast I thought of posting this so that you can have some idea and can try.
    – dh47
    Dec 23, 2014 at 9:53
  • 1
    eav_attribute_option_value this is the table which store attribute set values so please try fetching values from this table.
    – dh47
    Dec 23, 2014 at 9:55

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