【问题标题】:Magento Simple Product Page - Dynamically Update SKU with Custom OptionsMagento 简单产品页面 - 使用自定义选项动态更新 SKU
【发布时间】: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


【解决方案1】:

最好的办法是创建一个自定义模块。如果您不熟悉使用 Magento 创建模块,请查看此资源:https://www.smashingmagazine.com/2012/03/basics-creating-magento-module/

您还可以查看现有的 Magento 扩展来为您处理此问题:https://www.magentocommerce.com/magento-connect/

另一个想法是使用 JavaScript 来执行此操作。听起来这实际上可能很适合您,因为您只想更新前端(对吗??)。您可以创建一个 javascript 函数并在产品页面的不同方面添加 onchange 操作。在您的 JavaScript 中,您可以存储产品数组的数组或映射,例如 sku、价格、重量等......您可能希望在产品页面上动态更新的任何内容。

【讨论】:

  • 很好的建议。谢谢我的好朋友。我将采用JS方法。如果我需要后端功能,我当然也会尝试制作一个模块。
猜你喜欢
  • 2013-03-17
  • 1970-01-01
  • 2012-05-04
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-27
相关资源
最近更新 更多