Magento的属性选项框默认值
问题描述:
因为我发现在Magento选项框中默认值这里:Magento的属性选项框默认值
/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>
它显示“选择选项...”作为默认值
我试试
<script>
$(function() {
$("#attribute525 option:first-child").attr("selected", true); //color
$("#attribute272 option:first-child").attr("selected", true); //size
});
</script>
使默认值每个属性设置为第一个每个属性的选项,
但它不起作用。
答
在您的文件
app/design/frontend/default/mytheme/template/catalog/product/view/type/options/configurable.phtml
正下方
var spConfig = new Product.Config(< ?php echo $this->getJsonConfig() ?>);
粘贴下面JS代码
spConfig.setInitialState = function(dropdown_id)
{
//select dropdown
var dropdown = $(dropdown_id);
//remove empty option from dropdown so it is not selectable after initial selection
dropdown[0].remove();
//change selections in dropdowns
for(index = 0; index < dropdown.length; index++)
{
if(dropdown[index].value != "")
{
dropdown.selectedIndex = index;
var element = dropdown;
var event = 'change';
//fire events
if(document.createEventObject)
{
var evt = document.createEventObject();
return element.fireEvent('on'+event,evt)
}
else
{
var evt = document.createEvent("HTMLEvents");
evt.initEvent(event, true, true);
return !element.dispatchEvent(evt);
}
}
}
};
<?php foreach($_attributes as $_attribute): ?>
spConfig.setInitialState("attribute<?php echo $_attribute->getAttributeId() ?>")
<?php endforeach; ?>
这将使自动选择了产品视图页面
您好,感谢您对配置的选项为了答案,但它还是像以前一样向我展示'选择一个选项...',还有另一种方式,那就是不使用硬编码?欢呼声@Slimshadddyyy – user3558854
你可以在这里看到http://inchoo.net/ecommerce/magento/how-to-make-configurable-options-autoselected-on-configurable-product-view-page/ – Slimshadddyyy