I'm trying to call my custom JavaScript function in addtocart.phtml file, but it's not working.
After user clicked add to cart button, I'm calling my custom JavaScript function using , but not woring. For this, I tried below code.
Added below code to addtocart.phtml file from app/design/frontend/Vendor/Theme/Magento_Catalog/templates/product/view
<?php if ($block->isRedirectToCartEnabled()) : ?>
<script type="text/x-magento-init">
{
"#product_addtocart_form": {
"Magento_Catalog/product/view/validation": {
"radioCheckboxClosest": ".nested"
},
"ABCSolutions_CustomModule/js/custom": {
}
}
}
</script>
<?php else : ?>
And Custom Js file
define([
"jquery",
'Magento_Customer/js/customer-data',
"jquery/ui"
], function($,customerData) {
"use strict";
//creating jquery widget
$.widget('mage.custom', {
_create: function() {
//bind click event of elem id
this.element.on('click', function(e){
console.log('Click ME!');
console.log(customerData.get('customer'));
});
}
});
return $.mage.custom;
});
I tried both category and product page it's not working. Could you please suggest us to work above code for both pages after add to cart button clicked.
custom.jsfile.