How to override product.js file in magento

前端 未结 1 655
一个人的身影
一个人的身影 2021-02-03 12:25

I want to override the product.js file to add some extra values in some functions.I have copy the file from js/mage/adminhtml/product.js to my folder like js/myfolder/myproduct.

相关标签:
1条回答
  • 2021-02-03 13:13

    Let's say you're going to override function loadImage from js/mage/adminhtml/product.js in product edit page.

    Create your custom js: js/myfolder/myproduct.js:

    Product.Gallery.addMethods({
        loadImage : function(file) {
            alert('hi there');
            var image = this.getImageByFile(file);
            this.getFileElement(file, 'cell-image img').src = image.url;
            this.getFileElement(file, 'cell-image img').show();
            this.getFileElement(file, 'cell-image .place-holder').hide();
        }
    });
    

    Reference: http://prototypejs.org/learn/class-inheritance.html

    Then in your layout xml add your custom js:

    <adminhtml_catalog_product_edit>
        <reference name="head">
            <action method="addJs"><script>myfolder/myproduct.js</script></action>
        </reference>
    </adminhtml_catalog_product_edit>
    

    Using this method, function loadImage will be overridden only if you include your js/myfolder/myproduct.js.

    PS: Make sure js/myfolder/myproduct.js is included after js/mage/adminhtml/product.js (though it is like that by default since js/mage/adminhtml/product.js included in <default> tag)

    0 讨论(0)
提交回复
热议问题