Ajaxed Sliding Shopping Cart With jQuery
31 Jul // php the_time('Y') ?>

With this 5th & last post of the "WRD E-Commerce Week" we will be modifying "our Ajaxed shopping cart" to create a one which is very ideal for designs with limited spaces.
The shopping cart will:
- be hidden at the top of the page that can be displayed with a show/hide link
- open when an item is added to the basket & auto-close
- require a manual "hide" click if opened manually
- enable us to delete products

The HTML
For The Sliding Basket:
<div id="slidingTopWrap"> <div id="slidingTopContent"> <div id="basketWrap"> <div id="basketTitleWrap"> Your Basket <span id="notificationsLoader"></span> </div> <div id="basketItemsWrap"> <ul> <li></li> <?php getBasket(); ?> </ul> </div> </div> </div> <div id="slidingTopFooter"> <div id="slidingTopFooterLeft"> <img src="images/arrow-down.png" alt="Show Basket" /> <a href="no-js.htm" onclick="return false;" id="slidingTopTrigger">Show Basket</a> </div> </div> </div>
The Highlights
- the div
slidingTopWrapis positioned absolutely with top: 0 CSS value to stick to the top of the page - the div
slidingTopContentis hided via JavaScript when the page loads & it is actually the part which slides - the div
basketWrap, nothing unusual, wraps the contents of the basket
The JavaScript (jQuery)
Manual Show/Hide Part
$("#slidingTopContent").hide();
$("#slidingTopTrigger").live("click", function(event) {
$("#slidingTopContent").slideToggle("slow", function(){
if ($("#slidingTopContent").is(":visible")) {
$("#slidingTopFooterLeft").html('<img src="images/arrow-up.png" alt="Hide Basket" /> <a href="no-js.htm" onclick="return false;" id="slidingTopTrigger">Hide Basket</a>');
} else {
$("#slidingTopFooterLeft").html('<img src="images/arrow-down.png" alt="Show Basket" /> <a href="no-js.htm" onclick="return false;" id="slidingTopTrigger">Show Basket</a>');
}
});
});
The Highlights:
- we have
$("#slidingTopContent").hide();in the beginning to hide the div in the initial load - when the #slidingTopTrigger is clicked, we use the
slideTogglefunction to show/hide the #slidingTopContent. To understand whether it is open or not, we have an if then else clause asif ($("#slidingTopContent").is(":visible")) {- if the #slidingTopContent is visible, we insert the "hide HTML"
- if the #slidingTopContent is not visible, we insert the "show HTML"
- you’ll see that we used the
$("#slidingTopTrigger").live("click", function(event) {rather than a standard click function. The reason is adding the new created HTMLs to the DOM
Adding Products
$(".productPriceWrapRight a img").click(function() {
var productIDValSplitter = (this.id).split("_");
var productIDVal = productIDValSplitter[1];
if ($("#slidingTopContent").is(":visible")) {
$("#notificationsLoader").html('<img src="images/loader.gif">');
$.ajax({
type: "POST",
url: "inc/functions.php",
data: { productID: productIDVal, action: "addToBasket"},
success: function(theResponse) {
if( $("#productID_" + productIDVal).length > 0){
$("#productID_" + productIDVal).animate({ opacity: 0 }, 500);
$("#productID_" + productIDVal).before(theResponse).remove();
$("#productID_" + productIDVal).animate({ opacity: 0 }, 500);
$("#productID_" + productIDVal).animate({ opacity: 1 }, 500);
$("#notificationsLoader").empty();
} else {
$("#basketItemsWrap li:first").before(theResponse);
$("#basketItemsWrap li:first").hide();
$("#basketItemsWrap li:first").show("slow");
$("#notificationsLoader").empty();
}
}
});
} else {
$("#slidingTopContent").slideToggle("slow", function(){
$("#slidingTopFooterLeft").html('<img src="images/arrow-up.png" alt="Hide Basket" /> <a href="aaa.htm" onclick="return false;" id="slidingTopTrigger">Hide Basket</a>');
$("#notificationsLoader").html('<img src="images/loader.gif">');
$.ajax({
type: "POST",
url: "inc/functions.php",
data: { productID: productIDVal, action: "addToBasket"},
success: function(theResponse) {
if( $("#productID_" + productIDVal).length > 0){
$("#productID_" + productIDVal).animate({ opacity: 0 }, 500);
$("#productID_" + productIDVal).before(theResponse).remove();
$("#productID_" + productIDVal).animate({ opacity: 0 }, 500);
$("#productID_" + productIDVal).animate({ opacity: 1 }, 500);
$("#notificationsLoader").empty();
} else {
$("#basketItemsWrap li:first").before(theResponse);
$("#basketItemsWrap li:first").hide();
$("#basketItemsWrap li:first").show("slow");
$("#notificationsLoader").empty();
}
}
});
$("#slidingTopTrigger").fadeTo(4000, 1, function(){
$("#slidingTopContent").slideToggle("slow", function(){
$("#slidingTopFooterLeft").html('<img src="images/arrow-down.png" alt="Show Basket" /> <a href="aaa.htm" onclick="return false;" id="slidingTopTrigger">Show Basket</a>');
});
});
});
}
});
The Highlights:
As "how-to" on sending the Ajax query & fading animations are described in the previous post, we won’t repeating the same info here & focus on the effects.
- we again run the same if-then-else clause
- if the #slidingTopContent is open send an Ajax query to functions.php and add-the-products to basket
- if the #slidingTopContent is closed:
- slideToggle the div
- insert the "hide HTML" to #slidingTopFooterLeft"
- send an Ajax query to functions.php and add-the-products to basket
- wait for x seconds:
$("#slidingTopTrigger").fadeTo(4000, 1, function(){ - slideToggle the div
- insert the "show HTML" to #slidingTopFooterLeft"
P.S. To make the example work on your side, you should be creating a new database with the jBasket.sql file inside the download package & configure the database connection information inside “inc/db.php” file.

Requirements: jQuery 1.3+
- Tags: Javascript Php
- Filed under: E-Commerce, Extras, MIT License, Tutorials
- 14 Comments






















14 Responses for "Ajaxed Sliding Shopping Cart With jQuery"
Nice post, thx.
Anyway, i’m rss subscribed and yet cannot find the link for downloading, even for the previous post “Fly-To-Basket Effect With jQuery”.
Am i missing something?
@dejan,
Great that you liked it.
The download link is at: when viewing the post via RSS> at the bottom of the latest post>Under Special Downloads Title> Ajaxed Add-To-Basket Scenarios With jQuery And PHP
Kinda complicated I think
, maybe I should place them as a standard download or think something easier by still combining with RSS.
Good timing, I just built this into one of the sites I did: http://www.hotcakes.com/46 is an example.
It’s drag-drop however….er…well, It’s both I guess since you can click the “add to shopping list” from the item detail too…and compare area as well.
The idea is awesome and a great space saver.
Umut, thx.
The problem is on my side, since i thought that the link is referring to the previously posted script named “Fly-To-Basket Effect With jQuery”.
Thanks again
Hi, thanks for this tutorial.
But there seems to be a problem every time I push Hide basket it goes up but then down again – that means I have to push Show basket for it to go up. That’s strange.
But cool effect and I like it though.
@Brian,
Ok, I found when & why it happens:
Once a product is added to the cart, it slides and before it automatically hides, if you hide it manually then the auto-hide becomes, auto show.
I will add a simple if then else clause which will solve it.
[...] 13. AJAXed Sliding Shopping Cart Using jQuery [...]
[...] Ajaxed Sliding Shopping Cart With jQuery [...]
Hi, thanx for sharing)
very elegant and usefull
Hi all,
excellent script, although I came up with the following problem:
I have a form that picks up products options (radio buttons, checkboxes…) and then calculates a price of a product to be inserted in a shopping basket.
Since the present link is posting values via ‘a href’ tag through ‘functions.php?id=foo’ what should I change to get it done with submitting form??
any clues,
thanx in advance,
X
i am doing to mobile service center application ,
i want something like multiple combo
combo 1 ==> Mobilebrand
combo 2 ==> MobilebrandModel
combo 3 ==> Problem name
Display amount for this service,
Add to cart button .
How to customise the about cart …
Thanks
Thanks a lot for this nice script. If you have a lot of products on the same page and want the basket to auto-scroll with the page then a little CSS trick will do the trick without needing any extra JavaScript. Here is the code to change:
Go to file inc/style.css and change the following part:
#slidingTopWrap {
position: fixed; /* enable auto-scrolling of the basket with the page */
/* position: absolute; default-> basket fixed where it is! */
@Hakan,
Thanks very much for that, nice tip.
nice script….
thankx for sharing it…..