Code of a simple php + Ajax shopping cart program (1/2)

Source: Internet
Author: User
This article will recommend a good shopping cart effect. the main requirements here include a few things. one is written in php for the shopping cart class, and the other is jquery for Ajax operations, there is also a jquery plug-in thickbox. let's take a look...

This article will recommend a good shopping cart effect. the main requirements here include a few things. one is written in php for the shopping cart class, and the other is jquery for Ajax operations, there is also a jquery plug-in thickbox. let's take a look.

Shopping cart type: shop_cart.php
Shopping cart operation: cart_action.php
Home: index.html
Jquery is used for Ajax operations, and a jquery plug-in thickbox

Let's not talk about it anymore. you can first look at the effect example.
Shop_cart.php is of course the core of the shopping cart, but this class is very simple, because he introduced cart_action.php for external operations. So this class seems quite streamlined.
Shopping cart class shop_cart.php

The code is as follows:

Cart_name = $ name;
$ This-> items =$ _ SESSION [$ this-> cart_name];
}

/**
* SetItemQuantity ()-Set the quantity of an item.
*
* @ Param string $ order_code The order code of the item.
* @ Param int $ quantity The quantity.
*/
Function setItemQuantity ($ order_code, $ quantity ){
$ This-> items [$ order_code] = $ quantity;
}

/**
* GetItemPrice ()-Get the price of an item.
*
* @ Param string $ order_code The order code of the item.
* @ Return int The price.
*/
Function getItemPrice ($ order_code ){
// This is where the code taht retrieves prices
// Goes. We'll just say everything costs $9.99 for this tutorial.
Return 9.99;
}

/**
* GetItemName ()-Get the name of an item.
*
* @ Param string $ order_code The order code of the item.
*/
Function getItemName ($ order_code ){
// This is where the code that retrieves product names
// Goes. We'll just return something generic for this tutorial.
Return 'My Product ('. $ order_code .')';
}

/**
* GetItems ()-Get all items.
*
* @ Return array The items.
*/
Function getItems (){
Return $ this-> items;
}

/**
* HasItems ()-Checks to see if there are items in the cart.
*
* @ Return bool True if there are items.
*/
Function hasItems (){
Return (bool) $ this-> items;
}

/**
* GetItemQuantity ()-Get the quantity of an item in the cart.
*
* @ Param string $ order_code The order code.
* @ Return int The quantity.
*/
Function getItemQuantity ($ order_code ){
Return (int) $ this-> items [$ order_code];
}

/**
* Clean ()-Cleanup the cart contents. If any items have
* Quantity less than one, remove them.
*/
Function clean (){
Foreach ($ this-> items as $ order_code => $ quantity ){
If ($ quantity <1) unset ($ this-> items [$ order_code]);
}
}

/**
* Save ()-Saves the cart to a session variable.
*/
Function save (){
$ This-> clean ();
$ _ SESSION [$ this-> cart_name] = $ this-> items;
}
}

?>

For cart_action, it implements the intermediate role of the shop_cart class and index for updating, deleting, and adding commodity operations.
Cart_action.php

The code is as follows:

GetItemQuantity ($ _ GET ['Order _ Code']) + $ _ GET ['quantity '];
$ Cart-> setItemQuantity ($ _ GET ['Order _ Code'], $ quantity );
} Else {

If (! Empty ($ _ GET ['quantity ']) {
Foreach ($ _ GET ['quantity '] as $ order_code => $ quantity ){
$ Cart-> setItemQuantity ($ order_code, $ quantity );
}
}

If (! Empty ($ _ GET ['delete']) {
Foreach ($ _ GET ['delete'] as $ order_code ){
$ Cart-> setItemQuantity ($ order_code, 0 );
}
}
}
$ Cart-> save ();

Header ('Location: cart. php ');

?>

You can also perform external operations by using javasindex.html, that is, adding operations.

The code is as follows:





Shopping Cart

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.