首页 文章

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

提问于
浏览
1

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

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

configurable.phtml中的核心代码是:

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

?>
isSaleable()&& count($ _ attributes)):?>

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

</script>

3 回答

  • 1

    我上周刚刚在博客上写了关于an easy solution here的文章 . 我没有麻烦.1509406_麻烦 . 这就是我提出的,简而言之:


    /~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>
    

    如果您有兴趣,我的博文可以提供更多背景信息 .

  • 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();
            }
        });
    
  • 0

    以前的代码段只是产品页面上单个下拉列表属性的解决方案 . 这是多个下拉属性的解决方案 .

    首先编辑选项标记的标记,如下所示(app / design / frontend / [package] / [theme] /template/catalog/product/view/type/options/configurable.phtml):

    <option><?php echo $this->__('Choose %s...', $_attribute->getLabel()) ?></option>
    

    现在我们必须为JSON配置中的每个属性添加第一个选项的字符串,因为下拉项将在前端使用JavaScript创建 . 因此,我们遍历超级属性(与上面相同的文件):

    <script type="text/javascript">
       <?php
       $jsonConfig = json_decode($this->getJsonConfig());
       if (!empty($jsonConfig->attributes)) {
           foreach ($jsonConfig->attributes as $key => $attribute) {
               $jsonConfig->attributes->$key->chooseText = $this->__('Choose %s...', $attribute->label);
           }
       }
       ?>
       var spConfig = new Product.Config(<?php echo json_encode($jsonConfig); ?>);
    </script>
    

    现在扩展了JSON,但Magento JS不使用该条目 . 因此,我们需要在第172行修改文件js / varien / configurable.js.更改此信息:

    element.options[0].innerHTML = this.config.chooseText;
    

    成:

    element.options[0].innerHTML = this.config.attributes[attributeId].chooseText;
    

    现在,我们在页面加载,下拉列表更改和每页多个超级属性后都有正确的选项标签 .

相关问题