Event listeners for product and cart events

Event listeners for product and cart events

shopwithzac
Visitor
2 0 3

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 3 (3)

shopwithzac
Visitor
2 0 3

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

meh_coding
Visitor
1 0 1

 

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

 

zhicheng1
Shopify Partner
28 2 10

Dawn theme seems to publish events.  However, when I try to add event listeners to these events, nothing ever happens.

 

Here is the code in Dawn theme that fires the event when you add something to the cart:

 

in product-form.js around line 68:

publish(PUB_SUB_EVENTS.cartUpdate, { source: 'product-form', productVariantId: formData.get('id') });