将属性标签添加到Magento 1.6中的选项下拉列表中

时间:2012-09-27 12:52:31

标签: magento magento-1.6

我正在尝试在产品页面的“选择选项”下拉列表中添加属性标签,但没有硬编码,因为我可能会添加其他属性,所以例如我想显示“选择大小”或选择一种颜色“。

我玩过并试过各种论坛的代码,但似乎无法让它发挥作用 - 任何想法或任何建议的扩展

configurable.phtml中的核心代码是:

 <?php
 $_product    = $this->getProduct();
 $_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes());

&GT;

isSaleable()&amp;&amp;计数($ _属性)):&GT;?          
    <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">
                <option><?php echo $this->__('Choose Option')?></option> 
              </select>
          </div>
    </dd>
<?php endforeach; ?>
</dl>

        var spConfig = new Product.Config(getJsonConfig()?&gt;);

</script> 

2 个答案:

答案 0 :(得分:1)

问题是你的前端Javascript改变了选项文本是什么,你的PHP无法修复它。

但是你可以用一些前端代码做很多事情。

如果你只有一个可预测的选项,例如大小,你可以做这样的事情:

<script type="text/javascript">
    var spConfig = new Product.Config(<?php echo $this->getJsonConfig() ?>);
    // if only one drop down then set it to 'choose size'. If 'One Size' set value and hide
    if($$('select.super-attribute-select').length==1) {
        $$('select.super-attribute-select')[0].options[0].update('Choose Size');
        if($$('select.super-attribute-select')[0].options.length==2) {
            $$('select.super-attribute-select')[0].options[1].selected=true;
            $$('select.super-attribute-select')[0].up().hide();
        }
    }
</script>

如果只有一种尺寸,这也会隐藏下拉列表。

要扩展此方法并使其适用于任何下拉列表,您可能需要从页面中获取每个下拉列表的标签值:

    $$('select.super-attribute-select').each(function(element) {
    element.options[0].update('CHOOSE ' + element.up().up().previous().down().innerHTML.replace(/(<([^>]+)>)/ig,"").replace(/\*/g, '').toUpperCase());
    if(element.options.length==2) {
        element.options[1].selected=true;
        element.up().up().up().hide();
        }
    });

答案 1 :(得分:1)

我上周刚刚在博客上写了an easy solution here。我不想扩展核心代码,因为这很麻烦。这就是我提出的,简而言之:


/~theme/default/template/catalog/product/view/type/options/configurable.phtml

<?php
$jsonConfig = json_decode($this->getJsonConfig());
$jsonConfig->chooseText = 'Select ' . $_attribute->getLabel();
?>

<script type="text/javascript">
    var spConfig = new Product.Config(<?php echo json_encode($jsonConfig); ?>);
</script>

如果您有兴趣,我的博客文章会提供更多背景信息。

相关问题