Cart information and events with script-tag?

Highlighted
New Member
3 0 0

Hi,

 

A script-tag was added to the store.

 

From within this JavaScript script, how do I:

1. How do I tell how many items in the cart?

2. How do I tell when an item has been added to the cart (event)?

 

What options do I have in order to interact with the cart?

Looking for a client-side only solution.

 

Thank you.

0 Likes
Tourist
10 0 1

Hii

For cart item count you can use

{{ cart.item_count }

You can call cart object anywhere in theme code files.

And for add to cart event you need to create it by own. 

You can create javascript click event.

 

Thanks

 

gddeveloper
0 Likes
New Member
3 0 0

Looking for a client side only solution. A theme is server side..

 

You can create javascript click event

Into which event do I hook?

0 Likes
Shopify Partner
919 17 159

Yo, depends on your theme or if you're developing an app, then on the merchants theme.

Some themes add to cart via a form POST. Others use the Ajax API. Form submission can be easilly intercepted. For the Ajax API I suggest you play around with the sandbox and dig in to your theme's javascript to understand how it works.

Either way is possible, but there is no event you can hook in to as most if not all jQuery based themes I know of from Shopify don't work that way (i.e. don't use something like a global event bus or document bound events). So you'll have to wire this up yourself.

Best wishes!

I turn coffee in to code - since 1998
0 Likes
New Member
3 0 0

Thank you Karl.

0 Likes