I have the following code that triggers whenever the cart is updated.
The problem is that the jQuery version works as expected but the Vanilla JS doesn’t.
jQuery( document.body ).on('updated_cart_totals', function( event ) {
console.log( 'in here' ); // Works.
} );
document.body.addEventListener( 'updated_cart_totals', function( event ) {
console.log( 'in here too' ); // Doesn't work.
} );
Is there something I am missing?
2
Answers
It appears to be answered before
https://stackoverflow.com/a/41727891/5454218
jQuery uses its own system of event/data binding which inter-operates only one way. In WooCommerce context it’s an essential part of its system, so don’t shy away from it. No shame in using jQuery when you’re already deep in this pile of s**t.
My Answer would be, Yes it’s now possible to detect by
addAction()
anddoAction()
using Vanilla JS –wp.hooks.addAction()
.wp.hooks.doAction()
How to do – My Approach
Reference Example of WordPress Core
For reference, Let’s see a WordPress heartbeat js’s
doAction()
– https://github.com/WordPress/wordpress-develop/blob/trunk/src/js/_enqueues/wp/heartbeat.js#L460