Posted: April 26, 2013 at 12:24 pm
|
I’d like to be able to display a link to my site’s shopping cart, along with a count of how many items are currently in the cart. There doesn’t seem to be anything in the list of shortcodes for this, is there some native EE function to give me this data? |
|
Hi Hope, What a great idea! I have come up with a very rough idea on how it can be implemented. <?php $get_session = $events_in_session = isset( $_SESSION['espresso_session']['events_in_session'] ) ? $_SESSION['espresso_session']['events_in_session'] : event_espresso_clear_session( TRUE ); $count_session = count($_SESSION['espresso_session']['events_in_session']); if ($get_session == FALSE && $count_session == 1) { echo "0"; } else { echo $count_session; } ?> <script> jQuery('.ee_add_item_to_cart').click(function() { location.reload(); }); </script> What this does is get the session count for the cart and checks if the session is true or false (has an event or empty), if its false it echos 0 (as for some reason it will always echo 1 otherwise) and if it is true it will echo the number of events in the cart. The script forces a page refresh after the add to cart link is clicked, not ideal, but otherwise it wont show the latest count. I will pass this idea and my rough code onto the developers to see if it is something we can build into 4.0 as a shortcode or something. |
|
Oh I only tested this on the event list page, so you may need to tweak the code further if you are displaying it elsewhere. |
|
Thank you, that php works fine across all pages. I made it update dynamically using: [code language=”Javascript”] $(‘.ee_delete_item_from_cart’).click(function() { Where the cart count is in a span with the id "shopping-cart-count" contained by a span with the class "cart". Better than forcing a reload, but still not perfect if the ajax call fails. Maybe a future version of EE could include a callback in the registration shortcode so that the count within an element is incremented/decremented whenever an event is added/removed? |
|
That stripped out the span tags, let’s try that again:$('.ee_add_item_to_cart').click(function() { $('.ee_delete_item_from_cart').click(function() { |
|
Thanks for that Hope, seems your jQuery-fu is better than mine! |
The support post ‘Get number of items in cart’ is closed to new replies.
Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.