Skip to content

Magento2.4.7: How to get checkout error messages?

let’s assume I want to run a function when I click on place order.

I’m doing some functionality on the Place Order button click, but my functionality still works if there’s an error.

Is there a class or method which I can use in my custom js file to see if there’s any error then don’t execute my functionality,

I’m mixin stripe js file

var config = {
    config: {
        mixins: {
            'StripeIntegration_Payments/js/view/payment/method-renderer/stripe_payments': {
                'StripeIntegration_Payments/js/view/payment/method-renderer/stripe_payments-mixin': true
            }
        }
    }
};

This is my js file

I have clean the not relevant code just showing where I’m stuck

app/design/frontend/vendor/module/StripeIntegration_Payments/web/js/view/payment/method-renderer/stripe_payments-mixin.js

   define([
            'ko',
            'jquery',
            'Magento_Ui/js/modal/modal',
            'Magento_Ui/js/view/messages'  // Import messages component to use messageContainer
        ],
        function (ko, $, modal, Messages) {
     'use strict';

        return function (Component) {
            return Component.extend({
 showCustomModal: function () {
    // Get error messages from the messageContainer
                        var errorMessages = Messages().messageContainer.getErrorMessages();
    
                        // Loop through the error messages and check for any
                        for (var i = 0; i < errorMessages.length; i++) {
                            if (typeof errorMessages[i] === 'string') {
                                // If there's any error message, return true
                                return true;
                            }
                        }
                        // No error messages found
                        return false;
}
              });
        }
    });

But I got this error in console.

Uncaught TypeError: Cannot read properties of undefined (reading
‘getErrorMessages’)