Home > Backend Development > PHP Tutorial > A simple php+Ajax shopping cart program code (1/2)_PHP tutorial

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

The code is as follows Copy code
 代码如下 复制代码

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 .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;
}
}

?>

cart_name = $name;

$this->items = $_SESSION[$this->cart_name];
}

 代码如下 复制代码

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');

?>

/** * 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'); ?>

还有就是index.html实现对外的操作,也就是添加操作

 代码如下 复制代码
 代码如下 复制代码





Shopping Cart














Shopping Cart










There is also cart.php which is our shopping cart

 代码如下 复制代码
include('shopping_cart.class.php');
session_start();
$Cart = new Shopping_Cart('shopping_cart');
?>




Shopping Cart







Shopping Cart


hasItems() ) : ?>










$total_price = $i = 0;
foreach ( $Cart->getItems() as $order_code=>$quantity ) :
$total_price += $quantity*$Cart->getItemPrice($order_code);
?>
" : ""; ?>









数量 商品名称 商品编号 单价 总价 删除
getItemName($order_code); ?> $getItemPrice($order_code); ?> $getItemPrice($order_code)*$quantity); ?>
您的消费总金额是:¥




您还没有购物.



加载简单的购物车





1 2

www.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...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template