javascriptmagentomagento-1.6

Overriding/Extending the Magento core javascript files


Last days as a result of some customer complains and discussion with our marketing guys I've got a request to change the default behavior of the configurable products options. They asked me to remove the + $xx.xx from the options drop-down as it is confusing the customers/visitors and just leave the available options without displaying the price change. Fair enough from their point of view, but it is a bit tricky from developers point of view I think. The site is running Magento CE 1.6.2, and the file which we need to override/change is /public_html/js/varien/configurable.js. We need to change the getOptionLabel function in it so that it does not display the price change. So my question is: what is the right Magento way to change this file and not touch the core javascript file? Thanks in advance.


Solution

  • See this from prototype manual http://prototypejs.org/doc/latest/language/Function/prototype/wrap/ you can wrap whatever object method and even call "parent" if needed and here's a pseudo sample:

    //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
    });