2014-05-05 105 views
0

因爲我發現在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> 

使默認值每個屬性設置爲第一個每個屬性的選項,

但它不起作用。

回答

0

在您的文件

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; ?> 

這將使自動選擇了產品視圖頁面

+0

您好,感謝您對配置的選項爲了答案,但它還是像以前一樣向我展示'選擇一個選項...',還有另一種方式,那就是不使用硬編碼?歡呼聲@Slimshadddyyy – user3558854

+0

你可以在這裏看到http://inchoo.net/ecommerce/magento/how-to-make-configurable-options-autoselected-on-configurable-product-view-page/ – Slimshadddyyy