Event listeners for product and cart events

shopwithzac
New Member
2 0 1

Hi all,

 

I've been looking for any kind of documentation or reference material to find out what types of event listeners are available for Shopify events on the Online Store channel. So far I've stumbled across these two:

 

 

 

document.addEventListener('variant:changed', function(event) {
  // Do things
});

document.addEventListener('product:added', function(event) {
 // Do other things
});

 

 

 

I've tried guessing what other types of events there might be to no avail. I've also seen reference to other cart events - I'd be very interested in getting a list of all the product and cart events that are available!

 

Any assistance appreciated 😀

Replies 2 (2)
shopwithzac
New Member
2 0 1

*bump* anyone with any ideas or pointers for this?

meh_coding
New Member
1 0 0

 

Hey I'm using Debut theme, and I've accomplished this by finding the actions/functions in the theme.js, for example: "

_onRemoveItem or _addItemToCart etc.


I'm chaining an extra "then" promise (before catch) to the function, like:

.then(function(result) {
let event = new Event("customEvent", {bubbles: true}); // (2)
document.dispatchEvent(event);
})


Then in my custom.js file I can listen into that event with:

document.addEventListener("customEvent", function(event) {
//console.log('lost now found')
});


I don't like modifying the theme.js file tbh, but at least this way I keep the core edits to a minimum and can maintain my custom.js away from the core theme files as much as possible.

If you want to review events being fired, use chrome inspector Network tab and watch for XHR requests to find the "initiator" see the image:

That would be the initial ajax request that we need to append an extra then promise to.

I wish it were more simple!



Screen Shot 2022-09-18 at 12.56.17 AM.png