4

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

1

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

return function(originalShoppingCart){
        $.widget('mage.shoppingCart',originalShoppingCart, {
             //Overwrite function here.
        });
        return $.mage.shoppingCart;
}
  • With js file returned an Object: return { /* Code */ };

Must Return target variable

    return function (target) {
        var registerStep = target.registerStep;
        target.registerStep = function (code, alias, title, isVisible, navigate, sortOrder) {
            //Before Function.. 
            registerStep.apply(this,[code, alias, title, isVisible, navigate, sortOrder]); //Remove this line to skip call default function. 
            //After Function 
        };
        return target;
    };

Update: More detail: Rewrite JS in Magento 2

  • The first one (With js file returned $.mage.[widgetName]) worked for me. Thanks a lot. But be careful because if you use this, you will need to check if some file used this name or not. Ex: $.mage.shoppingCart is used, then you won't see any error in log file or chrome console but your function still not working. – fudu Feb 23 at 6:40
  • In case of comment recommended read this: solutiontutorials.com/development/magento-2/… – HoangHieu Feb 24 at 16:09
0

Kindly do like below code

define([
    'underscore',
    'Magento_Checkout/js/model/resource-url-manager',
    'Magento_Checkout/js/model/quote',
    'mage/storage',
    'Magento_Checkout/js/model/totals',
    'Magento_Checkout/js/model/error-processor',
    'Magento_Checkout/js/model/cart/cache',
    'Magento_Customer/js/customer-data',
], function (_, resourceUrlManager, quote, storage, totalsService, errorProcessor, cartCache, customerData) {
    'use strict';

    var mixin =
    {   

        estimateTotals: function (address) {
            var data = {
                shippingMethodCode: null,
                shippingCarrierCode: null
            };

            if (quote.shippingMethod() && quote.shippingMethod()['method_code']) {
                data.shippingMethodCode = quote.shippingMethod()['method_code'];
                data.shippingCarrierCode = quote.shippingMethod()['carrier_code'];
            }

            if (!cartCache.isChanged('cartVersion', customerData.get('cart')()['data_id']) &&
                !cartCache.isChanged('shippingMethodCode', data.shippingMethodCode) &&
                !cartCache.isChanged('shippingCarrierCode', data.shippingCarrierCode) &&
                !cartCache.isChanged('address', address) &&
                cartCache.get('totals')
            ) {
                quote.setTotals(cartCache.get('totals'));
            } else {
                loadFromServer(address);
            }
        }

    };

    return function (target) { // target == Result that Magento_Ui/.../default returns.
        return target.extend(mixin); // new result that all other modules receive 
    };

});

I have not tested but might be it will work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.