【问题标题】:Display Dynamic SKU on configurable product view Magento在可配置的产品视图 Magento 上显示动态 SKU
【发布时间】:2012-11-18 21:49:56
【问题描述】:

我有这个脚本可以在选择选项上显示动态 sku,但我无法开始工作。

正在加载正确的 sku,但在选择时没有任何反应。

此代码获取 Javascript 上的 sku 列表,并在可配置产品视图中为产品的选择选项更新 div。

<?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(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;?>


<?php
$conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
$col = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();

echo '<script type="text/javascript">';

echo '
document.observe("dom:loaded", function() {
  $("sku-container").update("<strong>Product Id: </strong> Select an option to display Product Id");
});
';
echo ' function changeSku(sel){';       

$itemId = array();           
foreach($col as $simple_product){
$itemId[] = array($simple_product->getSelectLabel() => $simple_product->getSku());
} 

//echo "<pre>";
//print_r($itemId);
//echo "</pre>";

foreach($itemId as $val){
 foreach($val as $k => $v){
echo "\n".'if(sel.options[sel.selectedIndex].value == "'.$k.'"){'."\n";
echo '$("sku-container").update("<strong>Product Id: </strong>'.$v.'");'. "\n";
echo '}';
    }
}

echo "\n".'if(sel.options[sel.selectedIndex].value == ""){'."\n";
echo '$("sku-container").update("<strong>Product Id: </strong> Select an option to display Product Id");'. "\n";
echo '}'; 

echo "}";
echo "\n</script>";
?>

感谢您的帮助。

谢谢

【问题讨论】:

    标签: magento dynamic


    【解决方案1】:

    除了$simple_product-&gt;getSelectLabel() 是一个错误的键之外,该脚本几乎是正确的。简单的产品模型中不存在这样的方法/属性。为了使脚本工作,这个键应该被替换为一个正确的 - 一个产品 ID。利用产品id,可以找到被选中产品的sku。


    所以,首先你需要重新组织itemId 数组,使其成为“productId => productSku”映射:

    $productMap = array();
    foreach($col as $simpleProduct){
        $productMap[$simpleProduct->getId()] = $simpleProduct->getSku();
    }
    


    然后您需要更改“onchange”函数调用以将 Configurable 的属性 id 传递给changeSku() 函数。因此,底层逻辑能够搜索适当的简单产品属性。

    onchange="return changeSku(<?php echo $_attribute->getAttributeId() ?>, this);">
    


    之后,您需要利用可配置的配置来将选定的简单产品的属性 id 映射到选定的产品 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");
        }
    }
    


    以下是整个模板的外观摘要供您参考(我已经美化了一点):

    <?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>
    

    这将完成您最初需要的任务。


    还要注意以下几点

    1. 您的方法不适用于具有两个或更多可配置属性的可配置产品。对于该产品,在用户为所有选择输入选择值之前,不知道最终的简单产品。所以应该改变一种方法,在输出 SKU 之前检查所有的选择。
    2. 当用户编辑产品配置而不是为新产品指定配置时,代码不考虑情况。您可以从购物车点击编辑链接进入编辑模式。在这种情况下,所有选择输入都将预先填充先前选择的值。但文本会显示“选择一个选项以显示产品 ID”。该脚本还可能在编辑模式下产生其他 Javascript 错误。代码应稍作修改以支持编辑模式。
    3. 模板充满了逻辑。 Magento 模板应该只有简单的打印和foreach-iterations。像$conf-&gt;getUsedProductCollection()-&gt;addAttributeToSelect('*')-&gt;addFilterByRequiredOptions() 这样的所有方法都最好移到阻止。这降低了代码复杂度。 希望对您有所帮助。

    【讨论】:

    • 谢谢安德烈,你救了我。
    • @Andrey Tserkus - 这太棒了。您是否有处理您解决的 3 个问题的示例?我一直不愿意找到处理超过 2 个可配置属性的扩展、插件或示例。另外,如果用户更改其中一个选项,我正在寻找可以更新 sku 的东西。任何指导将不胜感激。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    • 1970-01-01
    • 2012-12-03
    • 2013-03-17
    • 2015-11-25
    • 2016-01-24
    相关资源
    最近更新 更多