Magento 2 : How to fire custom ajax before add to cart ajax fire in product detail page?
-
19-02-2021 - |
Question
I have created custom extension and how to fire custom ajax before add to cart ajax fire in product detail page from my custom module. I don't want to override js in theme level. please suggest any solution
La solution
In your custom module (suppose my custom module is Anshu_Custom
), add requirejs-config.js
under Anshu/Custom/view/frontend
var config = {
config: {
mixins: {
'Magento_Catalog/js/catalog-add-to-cart': {
'Anshu_Custom/js/add-to-cart-mixin': true
}
}
}
};
Now create your mixin js file add-to-cart-mixin.js
under Anshu/Custom/view/frontend/web/js
define(['jquery'], function($){
'use strict';
return function(catalogAddToCart){ // this function receives the return value of the mixin target
$.widget('mage.catalogAddToCart', catalogAddToCart, {
submitForm: function(form) {
console.log('Working');
// your custom code
return this._super(form);
}
});
return catalogAddToCart; // always mixins has to return a function
};
});
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange