覆盖/扩展Magento核心javascript文件

时间:2012-08-13 19:48:57

标签: javascript magento magento-1.6

最后几天,由于一些客户抱怨并与我们的营销人员讨论,我已经请求更改可配置产品选项的默认行为。他们要求我从选项下拉菜单中删除+ $ xx.xx,因为它会让客户/访客感到困惑,只留下可用选项而不显示价格变化。从他们的观点来看还算公平,但从开发人员的角度来看,我认为这有点棘手。该站点正在运行Magento CE 1.6.2,我们需要覆盖/更改的文件是/public_html/js/varien/configurable.js。我们需要更改其中的getOptionLabel函数,以便它不显示价格变化。 所以我的问题是:什么是正确的Magento方式来更改此文件而不是触摸核心javascript文件? 提前谢谢。

3 个答案:

答案 0 :(得分:30)

从原型手册http://prototypejs.org/doc/latest/language/Function/prototype/wrap/中看到这个,你可以包装任何对象方法,甚至在需要时调用“parent”,这里是一个伪样本:

//where Product.Config is the object/class you need to "override"
Product.Config.prototype.getOptionLabel  = Product.Config.prototype.getOptionLabel.wrap(function(parentMethod){
    //replace the original method here with your own stuff
    //or call parentMethod(); if conditions don't match
});

答案 1 :(得分:26)

只是为了添加@ anton-s绝对正确的答案,你也可以做“完整”类重写:

// Create the original class
var ClassA = Class.create();
ClassA.prototype = {
    initialize: function(config) {
        this.config = config;
    },
    test: function(msg) {
        console.log('Hi from class A with message ' + msg);
    }
};

// Create new class extending the original class
var ClassB = Class.create(ClassA, {
    // $super is a reference to the original method
    test: function($super, msg) {
        console.log('Hi from class B');
        console.log('this.config is accessible in class B: ' + this.config);
        $super(msg + ' ...')
    }
});


// To make the extend an override, you can do this:
ClassA = ClassB;
// ClassA now is ClassB overriding the original ClassA
var a = new ClassA('some config data');
a.test('Call A 1');

因为所有这些只适用于原型类,而不是已经实例化的对象,我也会抛出这个hack,这几乎也是wrap()的作用:

// Overriding a method of an already instantiated object
// There are many ways to do this more elegantly thanks to the amazing JS scoping magic
a.origTest = a.test;
a.test = function(msg) {
    console.log('Hi from the patched method');
    this.origTest(msg);
}
a.test('Call A 2');

请记住,wrap()方法更好,也可用于类定义或具体实例。

// Wrap method of concrete instance
spConfig.getOptionLabel = spConfig.getOptionLabel.wrap(function(parentMethod, option, price) {
    return parentMethod(option, price);
});

// Wrap method of class declaration
Product.Config.prototype.getOptionLabel = Product.Config.prototype.getOptionLabel.wrap(function(parentMethod, option, price) {
    return parentMethod(option, price);
});

答案 2 :(得分:0)

如何在Magento 1.9 EE中覆盖\ js \ varien \ configurable.js并添加新的数据属性

创建文件\ js \ jsoverride \ configurable.js:

    Product.Config.prototype.reloadOptionLabels = Product.Config.prototype.reloadOptionLabels.wrap(function (parentMethod, element) {
    var selectedPrice;
    if (element.options[element.selectedIndex].config && !this.config.stablePrices) {
        selectedPrice = parseFloat(element.options[element.selectedIndex].config.price);
    } else {
        selectedPrice = 0;
    }
    for (var i = 0; i < element.options.length; i++) {
        if (element.options[i].config) {
            element.options[i].text = this.getOptionLabel(element.options[i].config, element.options[i].config.price - selectedPrice);
            element.options[i].setAttribute('data-in-stock', element.options[i].config.in_stock);
        }
    }
});

创建或使用文件:\ app \ design \ frontend \ enterprise \ YOUR_THEME \ layout \ local.xml并添加下一行:

<?xml version="1.0"?>
<layout version="0.1.0">
  <catalog_product_view>
    <reference name="head">
      <action method="addJs"><script>jsoverride/configurable.js</script></action>
    </reference>
  </catalog_product_view>
</layout>

请注意,将数据填充到文件

中的element.options [i] .config.in_stock中
  

app \ design \ frontend \ enterprise \ YOUR_THEME \ template \ catalog \ product \ view \ type \ options \ configurable.phtml

下一行

var spConfig = new Product.Config(UPDATED JSON WITH NEW ATTRIBUTE);
相关问题