我的脚本有一个小问题,就是选择每个产品的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"}});
感谢任何帮助。