A simple php+Ajax shopping cart program code (1/2)_PHP tutorial
WBOY
Release: 2016-07-13 16:56:59
Original
1060 people have browsed it
This article recommends a good shopping cart effect to everyone. The main requirements here include several things, one is the shopping cart class written in php, there is also an Ajax operation using jquery, and there is a jquery plug-in thickbox. Okay, let’s take a look.
Shopping cart category: shop_cart.php
Shopping cart operation: cart_action.php
Home page: index.html
Ajax operations use jquery, and there is also a jquery plug-in thickbox
No more details, you can take a look at the effect example
shop_cart.php is of course the core of the shopping cart, but this class is very simple because it also introduces cart_action.php for external operations. So this class seems quite streamlined.
Shopping cart class shop_cart.php
/**
* 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 .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 a
* 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;
}
}
if ( !empty($_GET['quantity']) ) {
foreach ( $_GET['quantity'] as $order_code=>$quantity){
$Cart->setItemQuantity($order_code, $quantity);
}
}
if ( !empty($_GET['remove']) ) {
foreach ( $_GET['remove'] as $order_code ) {
$Cart->setItemQuantity($order_code, 0);
}
}
}
$Cart->save();
header('Location: cart.php');
?>
/**
* 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 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 a
* 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 function between the shop_cart class and index, which is used to update, delete, and add products.
cart_action.php
The code is as follows
Copy code
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['remove']) ) {
foreach ( $_GET['remove'] as $order_code ) {
$Cart->setItemQuantity($order_code, 0);
}
}
}
$Cart->save();
header('Location: cart.php');
?>
http://www.bkjia.com/PHPjc/631557.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631557.htmlTechArticleThis article recommends a good shopping cart effect to everyone. The main requirements here include several things. One is The shopping cart class is written in php, there is also an Ajax operation using jquery, and there is another...
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn