可能重复:
在选择时将添加到购物车按钮更改为预购
我的脚本在选择每个产品的 sku 时遇到了一个小问题。
问题是,如果我添加 2 个下拉选项,jquery 将匹配相同的 id 两次。但是,如果我只有 1 个带有颜色或大小的下拉菜单,则 sku 映射显示正确。
我的问题是......我怎样才能获得第二种颜色的正确 sku?
<?php
$_product = $this->getProduct();
$_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes());
?>
<?php if ($_product->isSaleable() && count($_attributes)):?>
<dl>
<?php foreach($_attributes as $_attribute): ?>
<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"
onchange="return changeSku(<?php echo $_attribute->getAttributeId() ?>, this);">
<option><?php echo $this->__('Choose an Option...') ?></option>
</select>
</div>
</dd>
<?php endforeach; ?>
</dl>
<script type="text/javascript">
var spConfig = new Product.Config(<?php echo $this->getJsonConfig() ?>);
</script>
<div id="sku-container"></div>
<?php
$conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
$col = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();
$productMap = array();
foreach($col as $simpleProduct){
$productMap[$simpleProduct->getId()] = $simpleProduct->getAttributeText('sku');
}
?>
<script type="text/javascript">
document.observe("dom:loaded", function() {
$("sku-container").update("<strong>SKU: </strong> ...");
});
function changeSku(confAttributeId, sel) {
var productMap = <?php echo Mage::helper('core')->jsonEncode($productMap);?>;
var selectedAttributeId = sel.options[sel.selectedIndex].value;
if (selectedAttributeId) {
var options = spConfig.config.attributes[confAttributeId].options;
var productId = options.find(function (option) {return option.id == selectedAttributeId}).products[0]
$("sku-container").update("<strong>SKU: </strong>" + productMap[productId]);
} else {
$("sku-container").update("<strong>SKU: </strong> ...");
}
}
</script>
spConfig 结果:var spConfig = new Product.Config({"attributes":{"80":{"id":"80","code":"color","label":"Color","options":[{"id":"42","label":"Blue","price":"0","oldPrice":"0","products":["6873","6874"]},{"id":"46","label":"Green","price":"0","oldPrice":"0","products":["6881","6882","6883","6884","6885"]}]},"123":{"id":"123","code":"size","label":"Size","options":[{"id":"13","label":"4 UK","price":"0","oldPrice":"0","products":["6881"]},{"id":"12","label":"4.5 UK","price":"0","oldPrice":"0","products":["6873","6882"]},{"id":"11","label":"5 UK","price":"0","oldPrice":"0","products":["6874","6883"]},{"id":"10","label":"5.5 UK","price":"0","oldPrice":"0","products":["6884"]},{"id":"9","label":"6 UK","price":"0","oldPrice":"0","products":["6885"]}]}},"template":"#{price}\u00a0USD","basePrice":"489","oldPrice":"489","productId":"9844","chooseText":"Select\u0163i o op\tion...","taxConfig":{"includeTax":true,"showIncludeTax":true,"showBothPrices":false,"defaultTax":24,"currentTax":24,"inclTaxTitle":"Whit tax"}});
任何帮助表示赞赏。