【问题标题】:add attribute label to options dropdown in Magento 1.6将属性标签添加到 Magento 1.6 中的选项下拉列表
【发布时间】:2015-09-10 22:06:19
【问题描述】:

我正在尝试将属性标签添加到产品页面上的“选择选项”下拉列表中,但没有对其进行硬编码,因为我可能会添加其他属性,因此例如我想显示“选择尺寸”或选择一种颜色”。

我已经玩过并尝试了来自各种论坛的一些代码,但似乎无法让它工作 - 任何想法或任何建议的扩展

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> 

【问题讨论】:

    标签: magento magento-1.6


    【解决方案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>
    

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

    【讨论】:

    • 这是否也适用于多个属性?因为我正在尝试使用具有颜色和尺寸选项的产品。它显示颜色选项的选择尺寸。
    • @Ivekua - 是的,只有“chooseText”属性在 JSON 配置中发生了变化。我会尝试清除缓存。如果这不起作用,则可能是您正在使用的插件存在问题,但这会令人惊讶,因为此代码非常基础。
    【解决方案2】:

    问题是您的前端 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();
            }
        });
    

    【讨论】:

    • 感谢您的帮助,我使用了 Zachary Schuessler 选项,因为它看起来更简单...
    • 其实我也会这样做!感谢您的评论,因为我不会注意到。
    【解决方案3】:

    之前的sn-ps只是针对产品页面的单个下拉属性的解决方案。这是多个下拉属性的解决方案。

    首先像这样编辑选项标签的标记(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;
    

    现在我们在页面加载时、下拉更改后以及每个页面的多个超级属性中都有正确的选项标签。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-30
      • 1970-01-01
      • 2013-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多