Reputation: 102439
I am developing a shopping cart. Every Product
has a Price
and a SubCategory
.
ID Price Product No ProductName
1 250.5$ esp1 Electronic Machine
2 500.0$ esp2 Scanner
A user can choose the product by selecting any of the above Product
s.
There is also a CommonProduct
associated with each Product
.
Common_id Price Name
cs1 1.2$ Addional_Item1
cs2 5.0$ Additional_Item2
After a selection is made, the shopping cart will look like this:
Qty Name Price
1 Electronic Machine 256.7$
Additional_item1
Additional_item2
1 Scanner 505.0$
Additional_item2
I need implementation ideas for the above process. It is very tough for me to integrate Product
with Common Product
.
Is there a good way to do this using Ajax?
Upvotes: 4
Views: 2875
Reputation: 1602
Building on what Greg said, you could build a simple JavaScript object that looks a little like this...
Cart = function(){
this.add = function( productid ){
//jQuery Ajax method via PUT
}
this.remove = function( id ){
//jQuery Ajax method via DELETE
}
}
This CRUD object would be the JavaScript interface to your server side cart which could/should implement a RESTful interface.
As for displaying the common product, on the server returning an XML response, that contains the common products. This XML response for the adding the Electronic Machine could something like this:
<root>
<product price="250.0">Electronic Machine</product>
<commonproducts>
<commonproduct price="1.2">additional_item 1</commonproduct>
<commonproduct price="5.0">additional_item 2</commonproduct>
</commonproducts>
</root>
Use Case: Buy Sanner
Cart = new Cart();
//Do Ajax call
xmlResponse = Cart.add(1);
//Parse XML response and get an array of products
commonProducts = $(xmlResponse).find('commonProducts');
//Iterate over the array
$().each( commonProducts, function(v,i){
//Format them as you please
})
An alternative is to separate, this into two methods, and provide another interface for querying if a product has common products, and simply return a success or failure response to the CRUD methods.
Hopefully that was useful and made sense!
Upvotes: 2
Reputation: 893
You should create a database table to store the items in a customers shopping cart:
CartItem id(Primary Key), customer_id, product_id, quantity
And another table to store the common products for each item in the cart:
CartCommonItem cart_item_id(Primary Key), common_id(Primary Key)
And then make a script that generates JSON, XML or even HTML for your jQuery Ajax request.
Upvotes: 0
Reputation: 163
You can, but the better question here is should you?
In 2007, usability expert Jakob Neilsen studied AJAX carts and found that users mostly found them to be confusing.
Just a thought.
Upvotes: 0
Reputation: 6655
Just as a very general way of doing it:
Build web services with which to interface. Use JQuery to interact with the web services. Be careful to require some synchronicity to the methods. For example, you can't update quantity if the item is not in the cart, or you can increase quantity after the order is placed.
Upvotes: 1