【发布时间】:2015-09-17 15:08:51
【问题描述】:
我的产品页面有几个附加到购物车 sku 的自定义选项(下拉菜单)。但是,我想在选择选项时在产品页面上显示更新的 sku。
我找到了similar post about configurable items,但是,它不适用于我的情况,因为我正在使用带有选项的简单产品,不可配置产品。
如何在产品页面上显示和更新简单产品的 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>
<?php endif;?>
<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->getSku();
}
?>
<script type="text/javascript">
document.observe("dom:loaded", function() {
$("sku-container").update("<strong>Product Id: </strong> Select an option to display Product Id");
});
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>Product Id: </strong>" + productMap[productId]);
} else {
$("sku-container").update("<strong>Product Id: </strong> Select an option to display Product Id");
}
}
</script>
【问题讨论】:
-
顺便说一句,我正在使用 Magento 社区版 1.9.x
-
我敢肯定你有理由走你所做的方向,但看起来你肯定是在尝试重新发明轮子并反对 Magento 的内置数据结构。
-
@fantasticrice 我实际上并没有实现显示的代码,我将其用作参考。我正在寻找一种解决方案来显示简单产品的动态 sku。
-
我的意思是,这正是可配置产品类型的目的。根据选项选择特定的 SKU。因此,您正在尝试重新解决已解决的问题。
-
这是为了在产品页面上显示,而不是在购物车页面上。我希望在进行选择时(动态地)显示 sku。我已经在购物车/结账处拥有 Magento 生成的 sku;这很简单。我想知道如何在产品页面上展示它。
标签: javascript php jquery magento