How to extend js class/method of checkout model class in magento 2
-
13-12-2019 - |
Question
Basically I need to some method overwrite/extend of following Js class.
magento/module-checkout/view/frontend/web/js/model/step-navigator.js
Overwrite of this class through requirejs working fine. But I need to extend one of method of this class. here is an example of overwrite requirejs-config.js
var config = { "map": { "*": { "Magento_Checkout/js/model/step-navigator": "SR_Checkout/js/model/step-navigator" } } };
How to extend some method of any model class in checkout.
Solution
You need create requirejs-config.js in you module with
var config = {
config: {
mixins: {
'Magento_Checkout/js/model/step-navigator': {
'You_Module/js/step-navigator/pluggin': true
}
}
}
};
And js/step-navigator/pluggin.js file with content like
define(function () {
'use strict';
return function (target) { // target == Result that 'Magento_Checkout/js/model/step-navigator' returns.
// modify target
var navigateTo = target.navigateTo;
target.navigateTo = function(code, scrollToElementId) {
if (code == ...) { // before method
....
}
var result = navigateTo.apply(this, arguments);
//after method call
return result;
};
return target.
};
});
OTHER TIPS
This still works in 2.2.7. Thank you very much KAndy. You can also add veriables if you want to change it like this:
var result = navigateTo.apply(this, [code, scrollToElementId]);
//after method call
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange