I want to override the js file in module-checkout/view/frontend/web/js/model/cart/totals-processor/default.js
using mixins. Here is my requirejs code:
var config =
{
config:
{
mixins:
{
'Magento_Checkout/js/model/cart/totals-processor/default':
{
'Anowave_Ec/js/totals-processor/default/plugin': true
}
}
}
};
And here is my override file
define(function () {
'use strict';
var mixin = {
estimateTotals: function (address) {
var serviceUrl, payload;
totalsService.isLoading(true);
serviceUrl = resourceUrlManager.getUrlForTotalsEstimationForNewAddress(quote),
payload = {
addressInformation: {
address: _.pick(address, this.requiredFields)
}
};
if (quote.shippingMethod() && quote.shippingMethod()['method_code']) {
payload.addressInformation['shipping_method_code'] = quote.shippingMethod()['method_code'];
payload.addressInformation['shipping_carrier_code'] = quote.shippingMethod()['carrier_code'];
}
storage.post(
serviceUrl, JSON.stringify(payload), false
).done(
function (result) {
quote.setTotals(result);
/** I added new codes on this part **/
}
).fail(
function (response) {
errorProcessor.process(response);
}
).always(
function () {
totalsService.isLoading(false);
}
);
}
};
return function (target) { // target == Result that Magento_Ui/.../columns returns.
return target.extend(mixin); // new result that all other modules receive
};
});
I simply followed the documentation on Magento2 site but I'm getting errors like
target is not a function
Best Answer
These some ways to overwrite core Magento 2 JS using mixin
With js file returned $.mage.[widgetName]
Your target file must be using $.widget:
ex: source file: Magento_Checkout/js/shopping-cart
Must Return target variable
Update: More detail: Rewrite JS in Magento 2