UPDATE: There is a new version of simpleCart(js)
View the announcement of newest version (V3)

**update: v1.2: fixed rounding error in price formatting
**update: v1.1: back button works, no trailing comma for options

(if you want to skip right to the example and downloads, check it out here)

Over the past few years, our company has had several clients who have needed a smaller shopping cart, usually to sell just a couple of items. We generally built these small carts using paypal. I realized it would be nice to package this up, and create a lightweight, easy-to-use, flexible shopping cart.

So we made simpleCart(js). The cart is a 10.5kb javascript file that uses cookies to keep track of the items in the cart. SimpleCart(js) doesn’t require any databases or programming knowledge. You simply need to know some basic HTML and have the ability to copy and paste. However, it can easily be expanded to use databases or contain more advanced options.

Setup Paypal

The first thing you will need to do, if you don’t already have one, is set up a free merchant account with Paypal.

Add simpleCart(js) to your pages

Once you have the files downloaded, simply add this snippet to the top of any page you wish to have the cart running on:

[sourcecode language="html"]<br />
&lt;script src=&quot;simpleCart.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;<br />
&lt;script type=&quot;text/javascript&quot;&gt;&lt;!–<br />
simpleCart = new cart(&quot;you@yours.com&quot;);<br />
// –&gt;&lt;/script&gt;<br />
[/sourcecode]

You will need to replace you@yours.com with the email address you used to sign up to Paypal.

Adding items to the cart

Any page you have the simpleCart(js) running, you can add an item by simply using a link like this:

[sourcecode language="html"]<br />
&lt;a onclick=&quot;simpleCart.add(‘name=[name]‘,’price=[price]‘);return false;&quot; href=&quot;#&quot;&gt; link to add item &lt;/a&gt;<br />
[/sourcecode]

You are required to at least have a name and price when adding an item to the cart. However, you can add other fields also. If you want an image to be linked to the item, you can add it like this:

[sourcecode language="javascript"]<br />
simpleCart.add(‘name=Shirt’,’price=6.00′,’image=images/myImage.png’);<br />
[/sourcecode]

Viewing the cart

You may have the cart on any page, and it will update automatically using ajax. To show the cart items on a particular page, you simply need to have an element with the class of “simpleCart_items” on the page. This element will automatically be filled with all of the items in the cart, and will show each field.

[sourcecode language="javascript"]<br />
&lt;div class=&quot;simpleCart_items&quot;&gt;&lt;/div&gt;<br />
[/sourcecode]

You can use any element (div,a,span,p,etc.) and the values will be put in the innerHTML of that element.

Showing the totals on a page

You can show the total quantity or price on any page by simply having an element with a class “simpleCart_quantity” or “simpleCart_total”:

[sourcecode language="html"]<br />
&lt;div class=&quot;simpleCart_total&quot;&gt;&lt;/div&gt; &lt;span class=&quot;simpleCart_quantity&quot;&gt;&lt;/span&gt; items<br />
[/sourcecode]

Checkout and Empty Cart links

You can have a checkout or empty cart link anywhere on any page where the simpleCart(js) is active. You do this by simply setting the class of an element to “simpleCart_checkout” or “simpleCart_empty”:

[sourcecode language="html"]<br />
&lt;a class=&quot;simpleCart_checkout&quot; href=&quot;#&quot;&gt;checkout&lt;/a&gt;</p>
<p>&lt;a class=&quot;simpleCart_empty&quot; href=&quot;#&quot;&gt;empty cart&lt;/a&gt;<br />
[/sourcecode]

You can use any type of element for these, and you can put anything inside of the element. Anything wrapped in a tag with those classes will link appropriately.

Other Options

Adding other product options to items.

[sourcecode language="javascript"]<br />
simpleCart.add(‘name=Shirt’,’price=6.00′,’image=images/myImage.png’,’size=XL’,’color=Blue’);<br />
[/sourcecode]

You can also change the quantity that will be added to the cart:

[sourcecode language="javascript"]<br />
simpleCart.add(‘name=Shirt’,’price=6.00′,’image=images/myImage.png’,’quantity=5′,’size=XL’,’color=Blue’);<br />
[/sourcecode]

The order of the arguments doesn’t matter, and you can add as many options as you would like.

Styling the cart.

You can style this cart to fit the look of your page. The contents of the cart will start with a header row:

[sourcecode language="html"]<br />
&lt;div class=&quot;cartHeaders&quot;&gt;<br />
&lt;div class=&quot;itemImage&quot;&gt;Image&lt;/div&gt;<br />
&lt;div class=&quot;itemName&quot;&gt;Name&lt;/div&gt;<br />
&lt;div class=&quot;itemPrice&quot;&gt;Price&lt;/div&gt;<br />
&lt;div class=&quot;itemOptions&quot;&gt;Options&lt;/div&gt;<br />
&lt;div class=&quot;itemQuantity&quot;&gt;Quantity&lt;/div&gt;<br />
&lt;div class=&quot;itemTotal&quot;&gt;Total&lt;/div&gt;<br />
&lt;/div&gt;<br />
[/sourcecode]

Each item in the cart will have the following form:

[sourcecode language="html"]<br />
&lt;div class=&quot;itemContainer&quot;&gt;<br />
&lt;div class=&quot;itemImage&quot;&gt;[myImage]&lt;/div&gt;<br />
&lt;div class=&quot;itemName&quot;&gt;Shirt&lt;/div&gt;<br />
&lt;div class=&quot;itemPrice&quot;&gt;$6.00&lt;/div&gt;<br />
&lt;div class=&quot;itemOptions&quot;&gt;size: XL; color: Blue&lt;/div&gt;<br />
&lt;div class=&quot;itemQuantity&quot;&gt;&lt;input type=&quot;text&quot; /&gt;&lt;/div&gt;<br />
&lt;div class=&quot;itemTotal&quot;&gt;$30.00&lt;/div&gt;<br />
&lt;/div&gt;<br />
[/sourcecode]

And there will also be a totals row:

[sourcecode language="html"]<br />
&lt;div class=&quot;totalRow&quot;&gt;<br />
&lt;div class=&quot;totalItems&quot;&gt;6&lt;/div&gt;<br />
&lt;div class=&quot;totalPrice&quot;&gt;$30.00&lt;/div&gt;<br />
&lt;/div&gt;<br />
[/sourcecode]

You can choose to omit part of the cart in your stylesheet:

[sourcecode language="css"]<br />
.itemOptions,itemImage{<br />
display:none;<br />
}<br />
[/sourcecode]

Or you can hide the header or totals rows:

[sourcecode language="css"]<br />
.totalRow{<br />
display:none;<br />
}</p>
<p>.cartHeaders{<br />
display:none;<br />
}<br />
[/sourcecode]

Change the order of the cart columns.

If you would like to change the order of columns or remove the columns from ever being shown, simply add a line to the top of your page:

[sourcecode language="html"]<br />
&lt;script type=&quot;text/javascript&quot; src=&quot;simpleCart.js&quot;&gt;&lt;/script&gt;<br />
&lt;script type=&quot;text/javascript&quot;&gt;<br />
simpleCart = new cart(&quot;you@yours.com&quot;);</p>
<p>//Add the following line to reorder and remove item columns<br />
simpleCart.ItemColumns = ['Image','Price','Name','Quantity','Total'];</p>
<p>&lt;/script&gt;</p>
<p>[/sourcecode]

Download and Demo

Below are links to download simpleCart(js) and to view a working Demo. One great thing about the cart being stored in cookies is that you can have your items be added on one page and the cart be on another if you wish. The demo reflects this. The main page of the sample store is here and the View Your Cart page is here.

********* Please go to simplecartjs.org for the updated downloads and demo************