重複の可能性:
[カートに追加] ボタンを [選択時に予約注文] に変更
選択時にすべての製品の SKU を選択するスクリプトに小さな問題があります。
問題は、2 つのドロップダウン オプションを追加すると、jquery が同じ ID に 2 回一致することです。しかし、色またはサイズのドロップダウンが 1 つしかない場合、SKU は正しくマッピングされています。
私の質問は... どうすれば 2 番目の色の正しい 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"}});
どんな助けでも大歓迎です。