I need my app to listen on cart items add/remove/quantity change.
The merchant shouldn’t be required to past code or to drag blocks. And it should be compatible with all themes.
I’ve seen that this is possible and some apps like EG auto add to cart are build around this.
Hey again @Oussama_Khalfi - I think leveraging webhooks would likely be the best option here if you wanted to avoid having merchants integrate code themselves. There’s a bit more information here, but you’d likely be wanting to use the carts/update topic for this.
This would live outside of Liquid, but you could use the webhook as a trigger for app logic on your end. Hope this helps/makes sense - let me know if I can clarify anything on our end here.
Hi @Oussama_Khalfi - are you still seeing issues after following Alan’s advice, or can I mark this as solved?
Hi, i think webhooks are a solid solution, but not in my case i think
Since i want instant reaction to cart changes, and on the webhooks docs it says that webhooks doesn’t guarantee ordering
As with other webhook systems, Shopify doesn’t guarantee ordering within a topic, or across different topics for the same resource. For example, it’s possible that a
products/update
webhook might be delivered before aproducts/create
webhook.
So i used a simpler solution:
I override the window.fetch function so i can intercept /cart/change.js and /cart/add.js …
It’s consistent so far but i’m wondering if it will work for every theme.
What do you think?
Hey @Oussama_Khalfi - webhooks are usually a tiny bit slower, so I do get where you’re coming from here. I think your solution would work in most cases, but the variability of themes could cause possible hiccups like you mentioned there. Particularly if someone is implementing additional custom Javascript in addition to the AJAX Cart API.
Since window.fetch
is a global function in the browser, it’s also possible that the functionality could break if a customer/merchant is using a browser extension/plugin, etc that leverages that, so I did just want to mention that as well.
I think it could work, but there would be risks for sure. Other than webhooks, on our end, a theme app extension might be the best bet (this does require adding app blocks via the theme editor though): About theme app extensions
Hope this helps!