Support

Home Forums Multiple Event Registration Add-on Get number of items in cart

Get number of items in cart

Posted: April 26, 2013 at 12:24 pm

Viewing 5 reply threads


Hope Sullivan

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?


Dean

April 29, 2013 at 1:23 am

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.


Dean

April 29, 2013 at 1:23 am

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.


Hope Sullivan

April 29, 2013 at 7:47 am

Thank you, that php works fine across all pages. I made it update dynamically using:

[code language=”Javascript”]
$(‘.ee_add_item_to_cart’).click(function() {
if ($(‘#shopping-cart-count’).length > 0) {
var count = parseInt($(‘#shopping-cart-count’).text()) + 1;
$(‘#shopping-cart-count’).text(count);
}
else {
$(‘.cart’).append(‘1’);
}
});

$(‘.ee_delete_item_from_cart’).click(function() {
var count = parseInt($(‘#shopping-cart-count’).text());
if (count – 1 <= 0) {
$('#shopping-cart-count').remove();
}
else {
$('#shopping-cart-count').text(count – 1);
}
});
[/code]

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?


Hope Sullivan

April 29, 2013 at 7:50 am

That stripped out the span tags, let’s try that again:$('.ee_add_item_to_cart').click(function() {
if ($('#shopping-cart-count').length > 0) {
var count = parseInt($('#shopping-cart-count').text()) + 1;
$('#shopping-cart-count').text(count);
}
else {
$('.cart').append('<span id="shopping-cart-count">1</span>');
}
});

$('.ee_delete_item_from_cart').click(function() {
var count = parseInt($('#shopping-cart-count').text());
if (count – 1 <= 0) {
$('#shopping-cart-count').remove();
}
else {
$('#shopping-cart-count').text(count – 1);
}
});


Dean

April 30, 2013 at 2:08 am

Thanks for that Hope, seems your jQuery-fu is better than mine!

Viewing 5 reply threads

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.

Event Espresso