Archive: jQuery Smart Cart v2

jQuery shopping cart plugin with tabbed interface


Smart Cart is a flexible and feature rich jQuery plug-in for shopping cart. It makes the add-to-cart section of online shopping much easy and user friendly. It is compact in design, very easy to implement and only minimal HTML required.

The selected items in the cart are accessible as an array of product/quantity pair delimited with '|' on submit form or when checkout event is triggered. Smart Cart 2 have a simple and compact design with tabbed interface to saves space.

To avoid complexity I haven't implemented built-in Ajax support, but using the events you can easily implement the Ajax features. The product list is more dynamic with built-in search and category filter.
Template option gives you freedom to customize the product details display on product list and cart. It is also having option to show the product image and its expanded tool tip.

Downloads

Download jQuery Smart Cart v2.0.1

Features

  • Easy to implement, minimal HTML required
  • Clean and compact design, tabbed design saves space
  • Product search and Category filter enables easy and fast access to the product what users are looking for
  • Automatically calculates subtotal and total
  • Quantity is editable from the cart list
  • Event support, custom Ajax feature and validation can easily implement using the events
  • Product Image display and expanded tool tip display
  • Can implement multiple cart on same page
  • Template option, for easily customizing product display details
  • Pre-populate products on cart on page load

Documentation

Getting Started:

Basic Usage:
  $('#SmartCart').smartCart();
Using with option parameters:
  1. $('# SmartCart'). smartCart ({
  2. selected: 0, // 0 = produts list, 1 = cart
  3. resultName: 'products_selected[]',
  4. enableImage: true,
  5. enableImageTooltip: true,
  6. enableSearch: true,
  7. enableCategoryFilter: true,
  8. productItemTemplate:'<strong><%=pname%></strong><br />Category: <%=pcategory%><br />
  9. <small><%=pdesc%></small><br /><strong>
  10. Price: <%=pprice%></strong>',
  11. cartItemTemplate:'<strong><%=pname%></strong>',
  12.  
  13. // Events
  14. onAdd: null,
  15. onAdded: null,
  16. onRemove: null,
  17. onRemoved: null,
  18. onUpdate: null,
  19. onUpdated: null,
  20. onCheckout: null
  21. });
Parameters and Events are describing on the table below

Installing Smart Cart 2.0

Step 1: Include Files

Include the javascript and css files to your page
Files are:
  1. jQuery Library file (Don't include if already have it on your page)
  2. CSS(Style Sheet) file for Smart Cart
  3. Javascript plugin file for Smart Cart

To include the files copy and paste the below lines inside the <head> tag of your page.
Make sure the path to the files are correct with your working enviorment.
  1. <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
  2. <script type="text/javascript" src="js/jquery.smartCart-2.0.js"></script>
  3. <link href="styles/smart_cart.css" rel="stylesheet" type="text/css">

Step 2: The JavaScript

Inititialize the Smart Cart, copy and paste the below lines inside the <head> tag of your page
  1. <script type="text/javascript">
  2. $(document).ready(function(){
  3. $('#SmartCart').smartCart();
  4. });
  5. </script>

Step 3: The HTML

Finally the html, below shows the HTML markup for the Smart Cart,
Copy and paste the below html inside the <body> tag of your page.
  1. <div id="SmartCart" class="scMain">
  2. <input type="hidden" pimage="products/product1.jpg" pprice="2299.99"
  3. pdesc="" pcategory="Computers"
  4. pname="Apple MacBook Pro MA464LL/A 15.4" pid="100">
  5. <input type="hidden" pimage="products/product6.jpg" pprice="2699.99"
  6. pdesc="" pcategory="Computers"
  7. pname="Sony VAIO 11.1&quot; Notebook PC"
  8. pid="101">
  9. <input type="hidden" pimage="products/product3.jpg"
  10. pprice="550.00" pdesc="" pcategory="Cameras"
  11. pname="Canon Digital Rebel" pid="102">
  12. </div>
Note that there are some pseudo attributes added and they are used to describes about the product, like product name, price, description etc.

pid : Product ID
pname : Name of the product
pdesc : Description of the product
pprice : Price of the product
pimage : Product image source
pcategory : Category of the product

You can add more product details by adding new attributes to the input element, and so you can show them on product list or cart by editing the template.
You can customize the pseudo attribute names, and configure it on the Attribute Settings section of the plug-in file.

More details & descriptions:

1. How to show more product details on product list / How to customize Template?
  1. <input type="hidden" pimage="products/product1.jpg"
  2. pprice="2299.99" pdesc="The Intel Core Duo."
  3. pcategory="Computers" pname="Apple MacBook Pro" pid="100"
  4. product_sku="SDK334455">

Here added a new pseudo attribute "product_sku".
Describes below is how to apply product list template, and the highlighted part will be replaced with the SKU of specified for corresponding product.

Specify your attribute by wrapping it with <%= and %> on the templates (productItemTemplate, cartItemTemplate) will be replaced with the value of the attribute on the product list or cart when displayed.
Example: <%=YOUR_ATTRIBUTE_NAME%>
  1. var productTemplateWithSKU = '<strong><%=pname%></strong><br />Category: <%=pcategory%>
  2. <br /> <small><%=pdesc%></small><br /><strong>
  3. Price: <%=pprice%></strong><br />
  4. <strong>SKU: <%=product_sku%></strong>';
  5. // calling Smart Cart
  6. $('#SmartCart').smartCart({productItemTemplate: productTemplateWithSKU });
2. How to enable category filter?

Category filter is an automatic option and you can enable or disable by specifying the Option parameter: enableCategoryFilter to true/false
Also the product category attribute should present the hidden element, if it is empty or null the category filter will be disabled. The category select box is filled automatically by reading the category attribute.

So to enable category filter you have to do only 2 things:
1. Enable it on option parameter enableCategoryFilter : true (true by default).
2. Specify category attribute in the hidden element.
  1. <input type="hidden" pimage="products/product1.jpg" pprice="2299.99"
  2. pdesc="The Intel Core Duo." pcategory="Computers"
  3. pname="Apple MacBook Pro" pid="100">

Parameter Description:

Parameter Name Description Values Default Value
selected specify the selected tab 0 = product list
1= cart
0
resultName the name of the request to be passed when submitted
enableImage Enable/Disable Image display on product list and cart true = enabled
false= disabled
true
enableImageTooltip Enable/Disable Image tool tip display on product list and cart true = enabled
false= disabled
true
enableSearch Enable/Disable search for the products on product list true = enabled
false= disabled
true
enableCategoryFilter Enable/Disable category filter for the products on product list. Automatically disables if category attributes are not presents in the hidden elements true = enabled
false= disabled
true
productItemTemplate Template for the product display on product list String
(see details)
<strong><%=pname%></strong><br />
                Category: <%=pcategory%><br />

                <small><%=pdesc%></small><br />
                <strong>Price: <%=pprice%></strong>
cartItemTemplate Template for the product display on cart String
(see details)
<strong><%=pname%></strong>

 
 

Event Description:

Event Name Description Parameters Default
onAdd Triggered immediately when click on add to cart button and just before staring add to cart process. This is a decision making event, based on its function return value (true/false) the product add to cart process can be canceled or continued. Object: object of the hidden element of the product to add.
Quantity: The quantity of the product to add.
null
onAdded Triggered when click on add to cart button but only after finish adding the product to cart. This event doesn't accept return values. Object: object of the hidden element of the product added.
Quantity: The quantity of the product added.
null
onRemove Triggered immediately when a product is about to remove from cart, i.e. when click remove button on cart. This a decision making event, based on its function return value (true/false) the remove process can be canceled on continued. Object: object of the hidden element of the product to remove. null
onRemoved Triggered when the product remove (from cart) process is finished. This event doesn't accept return values. Object: object of the hidden element of the product removed. null
onUpdate Triggered when a product quantity is updated by changing it from cart. This a decision making event, based on its function return value (true/false) the update process can be canceled on continued. Object: object of the hidden element of the product to update.
Quantity: The quantity of the product to update
null
onUpdated Triggered when product quantity is finished updating. This event doesn't accept return values. Object: object of the hidden element of the product updated.
Quantity: The quantity of the product updated.
null
onCheckout Triggered when click on Checkout button on cart. If this even is not specified it will automatically submits the form element available. This event doesn't accept return values. Object: an object of the select element which contains option elements with value as the pair of product_id and quantity delimited with pipe '|' symbol. null

License

Released under the terms of the MIT License (more). You are free to use on both personal and commercial environment as long as the copyright header is left intact.

A little help is always a beautiful thing...

We are committed to deliver free and open source projects. Your support keep us on the track and make us deliver bigger things for you.

Dipu Raj

Dipu Raj

Full-stack developer in JavaScript, PHP and C# with added interest in Angular, React, jQuery and Bootstrap. Believes the code should be as beautiful as the design.