I need to override the particular function (for example : accountFieldsBind) of vendor/magento/module-sales/view/adminhtml/web/order/create/scripts.js
-
Have you got any solution?Dhaduk Mitesh– Dhaduk Mitesh2018-11-01 07:27:52 +00:00Commented Nov 1, 2018 at 7:27
-
Follow this magento.stackexchange.com/questions/272577/…Vipin Garg– Vipin Garg2020-03-03 06:21:01 +00:00Commented Mar 3, 2020 at 6:21
4 Answers
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
-
Thanks Ben, Yes I tried this but its not workingAmit Naraniwal– Amit Naraniwal2018-06-21 08:28:36 +00:00Commented Jun 21, 2018 at 8:28
-
@AmitNaraniwal did you manage to get it working?jibin george– jibin george2019-07-23 08:59:20 +00:00Commented Jul 23, 2019 at 8:59
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
-
1Have you an idea about how to override
_calcProductPricefunction in customscripts.jsfile (using mixins)?Dhaduk Mitesh– Dhaduk Mitesh2018-11-01 07:21:27 +00:00Commented Nov 1, 2018 at 7:21
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
-
2This overrides the entire file. OP's question was asking how to override a specific function.Darren Felton– Darren Felton2019-10-31 14:54:12 +00:00Commented Oct 31, 2019 at 14:54
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;
}
});