【问题标题】:Change the text "Choose an option..." on Magento product page更改 Magento 产品页面上的文本“选择一个选项...”
【发布时间】:2011-12-28 03:15:51
【问题描述】:

我创建了一个可配置的产品,它有三个选项:colorsizestyle

现在在产品页面中,每个选项在下拉菜单中都有默认文本“选择一个选项...”,但我希望文本应该是“选择颜色”、“选择尺寸”和“选择样式”。

我在 app\code\core\Mage\Catalog\Block\View\Type\Configurable.php 中编辑了函数 getJsonConfig()

发件人:

    'chooseText'        => Mage::helper('catalog')->__('Choose an Option...'),

收件人:

    'chooseText'        => ('Select ').$attribute->getLabel(),

并将文件frontend/base/default/template/catalog/product/view/type/options/configurable.phtml的第39行编辑为:

<option><?php echo $this->__('Select ') ?><?php echo $_attribute->getLabel() ?></option>

但是效果不好,在三个选项中总是显示“选择样式”的文字。 请给我这个问题的提示,非常感谢!

【问题讨论】:

    标签: magento drop-down-menu configurable


    【解决方案1】:

    我的版本同样的问题。您只需要更改模板 目录/产品/视图/类型/选项/configurable.phtml:

    <?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">
                    <?php $chooseText = $this->__('Select %s', $_attribute->getLabel()); ?>
                    <select data-choose-text="<?php echo $chooseText; ?>" name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select">
                        <option><?php echo $chooseText; ?></option>
                    </select>
                  </div>
            </dd>
        <?php endforeach; ?>
        </dl>
        <script type="text/javascript">
            Product.ConfigDefaultText = new Class.create(Product.Config, {
                fillSelect: function($super, element) {
                    $super(element);
                    var chooseDefaultText = element.getAttribute('data-choose-text');
                    $(element).options[0] = new Option(chooseDefaultText, '');
                }
            });
            var spConfig = new Product.ConfigDefaultText(<?php echo $this->getJsonConfig() ?>);
        </script>
    <?php endif;?>
    

    注释(摘自 cmets) 如果选择的默认值恰好不是“Select %s”,则替换

    $(element).options[0] = new Option(chooseDefaultText, '');
    

    $(element).options[0].innerHTML = chooseDefaultText;
    

    【讨论】:

    • 完美运行,修改非核心模板文件是最好的办法
    • 这将为第一个选择输入选择第一个非默认选项。这是故意的吗?有没有办法防止这种情况发生?
    • @Justin 好吧,我手头没有任何 Magento 安装,但据我所知,这段代码只更改了选择框中的第一个选项,通常是“选择一个选项”。如果为您选择了任何其他选项,则可能您的可配置属性具有另一个默认选项值,或者它恢复了用户前一段时间从会话或 smth 中选择的旧值。
    • 与贾斯汀有同样的问题。 :D
    • $(element).options[0].innerHTML = chooseDefaultText;这将确保如果 magento 没有指定任何其他规则,它将被选为默认值
    【解决方案2】:

    我一直在寻找一种更简单的方法来做到这一点。我不想扩展任何核心文件,也不想扩展 JavaScript。相反,我解析了 JSON 设置,更新了 chooseText 设置,然后转换回 JSON:


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

    <?php
    $jsonConfig = json_decode($this->getJsonConfig());
    $jsonConfig->chooseText = 'Select..';
    ?>
    
    <script type="text/javascript">
        var spConfig = new Product.Config(<?php echo json_encode($jsonConfig); ?>);
    </script>
    

    更多信息和更多示例here

    【讨论】:

      【解决方案3】:

      我认为唯一的方法就是修改填充下拉列表的 javascript 类。正如我们在frontend/base/default/template/catalog/product/view/type/options/configurable.phtml 中看到的那样,该类是:

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

      所需类的文件位于js/varien/product.js

      第一个&lt;option&gt;标签设置的地方是:

          fillSelect: function(element){
              var attributeId = element.id.replace(/[a-z]*/, '');
              var options = this.getAttributeOptions(attributeId);
              this.clearSelect(element);
              element.options[0] = new Option(this.config.chooseText, '');
              ...
      

      变量 chooseText 在第 368 行使用。该变量是在函数 getJsonConfig() 中的 app/code/core/Mage/Catalog/Block/Product/View/Type/Configurable.php 中创建的(您的挖掘方式是正确的)。您需要修改我之前描述的javascript 以实现您需要的内容(基于var attributeId,您可以将具有不同文本的选项分配给您需要的元素)

      【讨论】:

        【解决方案4】:

        如果您只更改文件可配置的.js
        它只会在页面加载时更改第一次选择
        所以我必须更改模板文件
        获取附件进行测试。(我只是将它写入一个小扩展)

        <?php
        $_product = $this->getProduct();
        $_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes());
        ?>
        <?php if ($_product->isSaleable() && count($_attributes)):?>
            <dl>
            <?php foreach($_attributes as $_attribute): ?>
                <?php
                $_attributeId = $_attribute->getAttributeId();
                $_attributeInfo = Mage::getModel('eav/entity_attribute')->load($_attributeId);
                $_attributeLabel = str_replace(' ','-',strtolower($_attributeInfo->getFrontendLabel()));
                ?>
                    <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 kevin-black-<?php echo $_attributeLabel;?>">
                            <option><?php echo $_attributeInfo->getFrontendLabel() ?></option>
                            </select>
                        </div>
                    </dd>
                <?php endforeach; ?>
            </dl>
        <script type="text/javascript">
            var spConfig = new Product.Config(<?php echo $this->getJsonConfig() ?>);
            //kevin.qazware@gmail.com Change Text follow attribute Label
            function changeFristText(){
                <?php foreach($_attributes as $_attribute): ?>
                    <?php
                    $_attributeId = $_attribute->getAttributeId();
                    $_attributeInfo = Mage::getModel('eav/entity_attribute')->load($_attributeId);
                    ?>
                    var label = '<?php echo $_attributeInfo->getFrontendLabel();?>';
                    $$('select.kevin-black-'+label).each(function(elem){
                        var options = elem.childElements();
                        options[0].update(label);
                    });
                <?php endforeach;?>
            }
        </script>
        <?php endif;?>
        
        
        in file : js/varien/configurable.js replace line 171 = element.options[0] = new Option(element.config.label, ‘’);
        

        它适用于所有属性集。

        【讨论】:

          【解决方案5】:

          最简单的答案:

          替换 js/varien/configurable.js 第 172 行

          element.options[0].innerHTML = 'Choose ' + this.config.attributes[attributeId].label;
          

          【讨论】:

          • 这对我来说看起来很合乎逻辑,因为无论我在 PHP 中编写什么,javascript 都会翻译,但唯一的问题是它对我不起作用。
          【解决方案6】:

          我通过这些代码扩展了类 Product.Config(方法 fillselect):

          fillSelect: function(element){
                              var attributeId = element.id.replace(/[a-z]*/, '');
                              var options = this.getAttributeOptions(attributeId);
                              this.clearSelect(element);
                                element.options[0] = new Option('Select '+element.config.label,'');
                              ........
          

          没关系!

          【讨论】:

            【解决方案7】:
                <script type="text/javascript">
                <?php
                    $jsonConfig = $this->getJsonConfig();
                    $jsonConfig = str_replace("Choose an Option...", "Select Size", $jsonConfig);
                ?>
                var spConfig = new Product.Config(<?php echo $jsonConfig; ?>);
            </script>
            

            【讨论】:

              【解决方案8】:

              文件目录/product/view/type/options/configurable.phml

              <?php
              $_product    = $this->getProduct();
              $_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes());
              ?>
              <?php if ($_product->isSaleable() && count($_attributes)):?>
                  <dl>
                  <?php foreach($_attributes as $_attribute): ?>
                      <?php 
                          $_attributeId = $_attribute->getAttributeId();
                          $_attributeInfo = Mage::getModel('eav/entity_attribute')->load($_attributeId);
                          $_attributeLabel = str_replace(' ','-',strtolower($_attributeInfo->getFrontendLabel()));
                      ?>
                      <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 kevin-black-<?php echo $_attributeLabel;?>">
                                  <option><?php echo $this->__('Select '.$_attributeLabel) ?></option>
                                </select>
                            </div>
                      </dd>
                  <?php endforeach; ?>
                  </dl>
                  <script type="text/javascript">
                      var spConfig = new Product.Config(<?php echo $this->getJsonConfig() ?>);
                      //Change Text follow attribute Label
                      function changeFristText(){
                          <?php foreach($_attributes as $_attribute): ?>
                              <?php 
                                  $_attributeId = $_attribute->getAttributeId();
                                  $_attributeInfo = Mage::getModel('eav/entity_attribute')->load($_attributeId);
                                  $_attributeLabel = str_replace(' ','-',strtolower($_attributeInfo->getFrontendLabel()));
                              ?>
                              var label = '<?php echo $_attributeLabel;?>';
                              $$('select.kevin-black-'+label).each(function(elem){
                                  var options = elem.childElements();
                                  options[0].update('Select ' + label);
                              });
                          <?php endforeach;?>
                      }
                  </script>
              <?php endif;?>
              

              并在文件 js/varien/configurable.js 中的第 171 行 (element.options[0] = new Option(this.config.chooseText, '');) 之后添加一行 changeFristText();

              它适用于所有属性集。

              【讨论】:

                【解决方案9】:

                这在 CE 1.8.1 上对我有用。它基于 Shein 的回答,并解决了加载时选择的错误选项。我基本上只是从 /js/varien/product.js 复制/粘贴 Product.Config.fillSelect() 方法。在粘贴的代码中我更改了:

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

                element.options[0].innerHTML = element.config.label;
                

                这允许 product.js 保持不变,而只是覆盖该方法。唯一的缺点是该方法的任何未来核心更新都需要迁移。

                由于新代码只是获得了“标签”设置,因此选择标签上不需要 data-choose-text 属性


                <?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">
                                    <option><?php echo $_attribute->getLabel() ?></option>
                                </select>
                              </div>
                        </dd>
                    <?php endforeach; ?>
                    </dl>
                    <script type="text/javascript">
                        Product.ConfigDefaultText = new Class.create(Product.Config, {
                            fillSelect: function (element) {
                                var attributeId = element.id.replace(/[a-z]*/, '');
                                var options = this.getAttributeOptions(attributeId);
                                this.clearSelect(element);
                                element.options[0] = new Option('', '');
                                element.options[0].innerHTML = element.config.label;
                
                                var prevConfig = false;
                                if (element.prevSetting) {
                                    prevConfig = element.prevSetting.options[element.prevSetting.selectedIndex];
                                }
                
                                if (options) {
                                    var index = 1;
                                    for (var i = 0; i < options.length; i++) {
                                        var allowedProducts = [];
                                        if (prevConfig) {
                                            for (var j = 0; j < options[i].products.length; j++) {
                                                if (prevConfig.config.allowedProducts
                                                    && prevConfig.config.allowedProducts.indexOf(options[i].products[j]) > -1) {
                                                    allowedProducts.push(options[i].products[j]);
                                                }
                                            }
                                        } else {
                                            allowedProducts = options[i].products.clone();
                                        }
                
                                        if (allowedProducts.size() > 0) {
                                            options[i].allowedProducts = allowedProducts;
                                            element.options[index] = new Option(this.getOptionLabel(options[i], options[i].price), options[i].id);
                                            element.options[index].config = options[i];
                                            index++;
                                        }
                                    }
                                }
                            }
                        });
                        var spConfig = new Product.ConfigDefaultText(<?php echo $this->getJsonConfig() ?>);
                    </script>
                <?php endif;?>
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2014-10-03
                  相关资源
                  最近更新 更多