更改文本“;选择一个选项……”;在Magento产品页面上

更改文本“;选择一个选项……”;在Magento产品页面上,magento,drop-down-menu,configurable,Magento,Drop Down Menu,Configurable,我创建了一个可配置的产品,它有三个选项:颜色、大小和样式。 现在在产品页面中,每个选项在下拉列表中都有默认文本“选择一个选项…”,但我希望文本应该是“选择颜色”、“选择大小””和“选择样式” 我在app\code\core\Mage\Catalog\Block\View\Type\Configurable.php中编辑了函数getJsonConfig() 发件人: 致: 并将文件frontend/base/default/template/catalog/product/view/type/op

我创建了一个可配置的产品,它有三个选项:颜色大小样式

现在在产品页面中,每个选项在下拉列表中都有默认文本“选择一个选项…”,但我希望文本应该是“选择颜色”、“选择大小””和“选择样式”

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

发件人:

致:

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

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

但是效果并不好,它总是在三个选项中显示文本“选择样式”。
关于这个问题,请给我一个提示,非常感谢

我认为唯一的方法就是修改填充下拉列表的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>

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

I extended class 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,'');
                    ........

没关系

文件目录/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;?>

class=“last”>
);
//更改属性标签后面的文本
函数changeFristText(){
var标签=“”;
$$('select.kevin black-'+标签)。每个(函数(elem){
var options=elem.childElements();
选项[0]。更新('选择'+标签);
});
}
并添加一行
changeFristText()在文件js/varien/configurable.js的第171行之后(
element.options[0]=新选项(this.config.chooseText.);


它适用于所有属性集。

如果只更改file configurable.js
只有在页面加载时,它才会更改“第一次选择”
所以我必须更改模板文件
获取用于测试的附加文件。(我只是将其写入一个小扩展名)


class=“last”>

我正在寻找一种更简单的方法来实现这一点。我不想扩展任何核心文件,也不想用扩展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>

var spConfig=new Product.Config();

更多信息和更多示例。


var spConfig=new Product.Config();

我对同一问题的看法。您只需要更改模板 目录/产品/视图/类型/选项/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;?>

最简单的答案是:

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

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

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

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

这允许不修改product.js,只需重写该方法即可。唯一的缺点是将来对该方法的任何核心更新都需要迁移

由于新代码只获得“label”设置,因此在select标记上不需要数据choosetext属性on



*
class=“last”>

工作正常,修改非核心模板文件是最好的方法。这将为第一个选择输入选择第一个非默认选项。这是有意的吗?有什么方法可以防止吗?@Justin嗯,我手头没有任何Magento安装,但据我所知,这段代码只更改了selectbox中的第一个选项,通常是“选择一个选项”。如果为您选择了任何其他选项,可能您的可配置属性有另一个默认选项值,或者它恢复了用户不久前从session或smth中选择的旧值。工作正常,但与Justin的问题相同:D$(元素)。选项[0]。innerHTML=chooseDefaultText;这将确保如果magento没有指定任何其他规则,它将被选为默认值。这在我看来是逻辑的,因为javascript无论如何都会翻译我在PHP中编写的内容,但唯一的问题是它对我不起作用。
<?php
$jsonConfig = json_decode($this->getJsonConfig());
$jsonConfig->chooseText = 'Select..';
?>

<script type="text/javascript">
    var spConfig = new Product.Config(<?php echo json_encode($jsonConfig); ?>);
</script>
    <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>
<?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;?>
$(element).options[0] = new Option(chooseDefaultText, '');
$(element).options[0].innerHTML = chooseDefaultText;
element.options[0].innerHTML = 'Choose ' + this.config.attributes[attributeId].label;
element.options[0].innerHTML = this.config.chooseText;
element.options[0].innerHTML = element.config.label;
<?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;?>