天天看點

magento -- 可配置産品的選項如何預設選中第一項

if you ever tried to do anything with magento configurable products view page, most likely you needed changes in  /js/varien/product.js in case you wanted to manipulate dropdowns.

this will be one of the ways to do it.

basically, what we need to do in order to make initial selection of a product is the following:

open this file: /app/design/frontend/default/your_theme/template/catalog/product/view/type/options/configurable.phtml

right below

var spconfig = new product.config(< ?php echo $this->getjsonconfig() ?>); 

add this javascript code:

//we create new function

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

that’s it. i hope you can find this usable, however don’t use it on production site without extensive testing.

as you can see, all prototype functions in magento (and in general) can be added as new into already existing class.

same way you could override existing methods in existing classes.

i have coded this feature for the purpose of this article and i’m not claiming that it is production ready. it is only for informative purposes.

原文連結位址:http://inchoo.net/ecommerce/magento/how-to-make-configurable-options-autoselected-on-configurable-product-view-page/