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.
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);
}
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.
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;If you need to edit the prototype of an object use:
Product.OptionsPrice.prototype.formatPrice = myFormatPrice;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.