How do I fetch visitors cart content count, using javascript?

I need to add a counter to a link to "view cart" - to show how many items are in the users cart.

I would like to be able to cache all pages, except "cart view" and /checkout/* - using varnish.

So I would like to fetch this counter of items in users cart - using services API.

However, I'm not sure how. I would need to call using some ajax code - which would also send sessionid along in the call - so the services implementation ( I was thinking of using services-3.x module) can see which users cart it's suppose to fetch data from.

I have found dc_ajax_cart module - but I only want a counter - so I figure it's not covering this (since it "just" works with the cart block, generated from a view).

Any hints would be very much appreciated.

Posted: Jul 6, 2012


klavs on July 6, 2012

It would seem I could use commerce_cart_order_load() function to fetch data - but it requires that there's a session to identify the anonymous user from.

I that automaticly available if I use $.ajax or some other javascript get function client-side - to a url, which would then trigger my function, which calls commerce_cart_order_load ?

klavs on July 6, 2012

the answer is yes - to session being available (me wakes up). Now to try to strap on the services module and see how that goes :)