11

I need to override the particular function (for example : accountFieldsBind) of vendor/magento/module-sales/view/adminhtml/web/order/create/scripts.js

Amit Naraniwal
  • 1,298
  • 10
  • 20

4 Answers4

8

Add the following to your Require JS config:

var config = {
 config: {
     mixins: {
         'Module_Name/js/path/to/js/file': {
             'Your_Theme/js/path/to/new/js/file': true
         }
     }
 }
};

And inside Your_Theme/js/path/to/new/js/file.js you need to return the function(s) you need to extend. How you do this depends on how the file was written, for example if it's using object literal JS or it's just a singular function.

In my example below I'm extending/overriding a function from a UI component, this._super(); is important to note as this will runthe original function. So if you need to extend the function and do additional logic you can use this, if you want to fully overwrite the function you can leave it out.

define([
    'jquery'
], function ($) {
    'use strict';

    return function (Component) {
        return Component.extend({

            functionYouAreOverriding: function () {
               this._super(); // This will run the original function, you may or may not need this.

                //... Your new code
            }
        });
    }
});

I'm not sure on the exact syntax you need to use as the file you mention uses AdminOrder.prototype, my guess would be:

return AdminOrder.prototype = {
    functionToOverride: function() {
        ...
    }
}

For more info see the official dev docs - https://devdocs.magento.com/guides/v2.2/javascript-dev-guide/javascript/js_mixins.html

Ben Crook
  • 15,685
  • 3
  • 52
  • 104
0

Create a new module and add a reqired-config.js in

MyVendor/MyModule/view/frontend/requirejs-config.js

requirejs-config.js

var config = {
    "map": {
        "*": {
            "Magento_Sales/order/create/scripts": "MyVendor_MyModule/js/order/create/scripts",
        }
    }
}

And create

MyVendor/MyModule/view/frontend/web/js/order/create/scripts.js

In you module and this can override the file

Chirag Patel
  • 6,126
  • 2
  • 23
  • 65
govindaraj
  • 115
  • 1
  • 10
0

Try following way

var config = {
    config: {
        mixins: {
            'Magento_Sales/order/create/scripts': {
                'MyVendor_myModule/js/order/create/scriptUpdate': true
            }
        }
    }
};

OR

var config = {
    "map": {
        "*": {
            "Magento_Sales/order/create/scripts": "MyVendor_myModule/js/order/create/scripts",
        }
    }
}

In that case your overwrite js location

MyVendor/myModule/view/adminhtml/web/js/order/create/scripts.js
Prashant Patel
  • 1,291
  • 11
  • 21
0

Follow below URL How to extend admin js in magento 2 by mixins

It will solve the problem.

/* global AdminOrder */
define([
    'jquery',
    'Magento_Sales/order/create/scripts'
], function (jQuery) {
    'use strict';

    AdminOrder.prototype.loadShippingRates = function () {
        var addressContainer = this.shippingAsBilling ?
            'billingAddressContainer' :
            'shippingAddressContainer',
            data = this.serializeData(this[addressContainer]).toObject();

        data['shipto_type'] = document.querySelector("#shipto_type").value;
        data['liftgate'] = document.querySelector("#liftgate").value;

        data['collect_shipping_rates'] = 1;
        this.isShippingMethodReseted = false;
        this.loadArea(['shipping_method', 'totals'], true, data);

        return false;
    }
});
kunj
  • 3,699
  • 2
  • 10
  • 25
Vipin Garg
  • 244
  • 2
  • 12