如何覆盖js / varien / product.js中的JS函数? [英] How do I override a JS function in js/varien/product.js?
问题描述
我正在制作一个Magento扩展,在产品视图页面上调用自定义JS文件。这个自定义JS文件将加载最后一个,需要覆盖/js/varien/product.js底部的formatPrice()函数。
I am making a Magento extension that calls a custom JS file on the product view page. This custom JS file will load last and needs to override the formatPrice() function found at the bottom of /js/varien/product.js.
原始的formatPrice函数是如下:
The original formatPrice function is as follows:
formatPrice: function(price) {
return formatCurrency(price, this.priceFormat);
}
我想用以下内容替换/覆盖此函数:
I would like to replace / override this function with the following:
formatPrice: function(price) {
if (price % 1 == 0) { this.priceFormat.requiredPrecision = 0; }
return formatCurrency(price, this.priceFormat);
}
如何在我的自定义JS文件中编写JS代码,以便它能够正确覆盖此功能?我不熟悉JS足以知道。
How do I write the JS code in my custom JS file so that it will properly override this function? I'm not familiar with JS enough to know.
推荐答案
如果它是全局的那么你可以做 window.formatPrice = myNewFormatPrice;
如果它是一个对象的成员,那么你会做类似的事情: anObject.formatPrice = myNewFormatPrice;
If it is global then you can just do window.formatPrice = myNewFormatPrice;
if it is a member of an object then you would do something like: anObject.formatPrice = myNewFormatPrice;
如果您需要编辑对象的原型,请使用: Product.OptionsPrice.prototype.formatPrice = myFormatPrice;
If you need to edit the prototype of an object use: Product.OptionsPrice.prototype.formatPrice = myFormatPrice;
此外,您还需要查看 requiredPrecision
的访问权限。如果它是私人或受保护,那么您将无法访问它。
Also you need to look into the access to requiredPrecision
. If it is "private" or "protected" then you won't be able to access it.
这篇关于如何覆盖js / varien / product.js中的JS函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!