0

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')

asked Oct 22, 2024 at 13:03

1 Answer 1

0

After much debugging, I found that there is also a message container in the parent class. So I just needed to call that function in my mixing and it start working,

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 msgs = this.messageContainer.errorMessages();
 // Check if there are any error messages
 if (msgs.length > 0) {
 // Prevent the modal from opening if errors are present
 return true;
 }
 // No error messages found
 return false;
 }
 });
 }
 });
answered Oct 22, 2024 at 13:32

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.