I tried to overwrite Magento_Catalog/js/catalog-add-to-cart.js only to extend the submitForm method as follows:
app/code/Vendor/Module/view/frontend/requirejs-config.js
/**
* Copyright © Onectus, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
var config = {
map: {
'*': {
productIframe: 'Vendor_Module/js/product-iframe',
}
},
config: {
mixins: {
'Magento_Catalog/js/catalog-add-to-cart': {
'Vendor_Module/js/catalog-add-to-cart-mixin': true
}
}
}
};
app/code/Vendor/Module/view/frontend/web/js/catalog-add-to-cart-mixin.js
/**
* Copyright © Onectus, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
define([
'jquery',
'mage/translate'
], function($, $t) {
"use strict";
return function(widget) {
$.widget('mage.catalogAddToCart', widget, {
/**
* Handler for the form 'submit' event
*
* @param {Object} form
*/
submitForm: function(form) {
alert('Here!');
return false;
...
}
});
return $.mage.catalogAddToCart;
}
});
My problem is that sometimes the JS file is loaded in the DOM, but sometimes is not loaded and I don’t know why. I need it to be loaded each time so that I can do some validation before calling this.ajaxSubmit(form).
Is this a dependency-related issue?
What am I doing wrong?