


A relatively complete shopping cart class implemented in PHP, php shopping cart class_PHP tutorial
A relatively complete shopping cart class implemented in PHP, php shopping cart class
The example in this article describes a relatively complete shopping cart class implemented in PHP. Share it with everyone for your reference. The specific implementation method is as follows:
I recently completed a project that required the use of a shopping cart. Considering that it may be used frequently, I encapsulated it into a class so that it can be called later. Interested readers can simply modify this class slightly and use it. It’s in my own program.
/* /* file type: Contains files, the recommended suffix is .inc /* /* file name: cart.inc /* /* Description: Define a car purchase category */
/* /* Func list: class cart /* /* author: bigeagle /* /* /*****************************************************************************/
//Define constants for this file
define("_CART_INC_" , "exists") ;
/*Shopping cart class*/
class TCart
{
var $SortCount; //Number of product categories
var $TotalCost; //Total value of goods
var $Id; //ID of each type of product (array)
var $Name; //The name of each type of product (array)
var $Price; //The price of each type of product (array)
var $Discount; //Product discount (array)
var $GoodPrice ; //The preferential price of the product (array)
var $Count; //The number of pieces of each type of product (array)
var $MaxCount ; //Product limit (array)
//******构造函数
function TCart()
{
$this->SortCount=0;
session_start(); //初始化一个session
session_register('sId');
session_register('sName');
session_register('sPrice');
session_register('sDiscount');
session_register('sGoodPrice') ;
session_register('sCount') ;
session_register('sMaxCount') ;
$this->Update();
$this->Calculate();
}
//********私有,根据session的值更新类中相应数据
function Update()
{
global $sId,$sName,$sPrice,$sCount,$sDiscount,$sMaxCount,$sGoodPrice;
if(!isset($sId) or !isset($sName) or !isset($sPrice)
or !isset($sDiscount) or !isset($sMaxCount)
or !isset($sGoodPrice) or !isset($sCount)) return;
$this->Id =$sId;
$this->Name =$sName;
$this->Price =$sPrice;
$this->Count =$sCount;
$this->Discount = $sDiscount ;
$this->GoodPrice = $sGoodPrice ;
$this->MaxCount = $sMaxCount ;
//计算商品总数
$this->SortCount=count($sId);
}
//********私有,根据新的数据计算每类商品的价值及全部商品的总价
function Calculate()
{
for($i=0;$i<$this->SortCount;$i++)
{
/*计算每件商品的价值,如果折扣是0 ,则为优惠价格*/
$GiftPrice = ($this->Discount[$i] == 0 ? $this->GoodPrice :
ceil($this->Price[$i] * $this->Discount[$i])/100 );
$this->TotalCost += $GiftPrice * $this->Count[$i] ;
}
}
//**************The following are interface functions
//*** Add an item
// Determine whether there is already one in the blue, if so, add count, otherwise add a new product
//First, change the value of the session, and then call update() and calculate() to update the member variables
function Add($a_ID , $a_Name , $a_Price , $a_Discount ,
$a_GoodPrice , $a_MaxCount , $a_Count)
{
global $sId , $sName , $sCount , $sPrice , $sDiscount ,
$sGoodPrice , $sMaxCount ;
$k=count($sId);
for ($i=0; $i<$k; $i++)
{ //First check if this product has been added
If($sId[$i]==$a_ID)
{
$sCount[$i] += $a_Count ;
break;
}
}
if($i >= $k)
{ //If not, add a new product category
$sId[] = $a_ID;
$sName[] = $a_Name;
$sPrice[] = $a_Price;
$sCount[] = $a_Count;
$sGoodPrice[] = $a_GoodPrice;
$sDiscount[] = $a_Discount;
$sMaxCount[] = $a_MaxCount;
}
$this->Update(); //Update the member data of the class
$this->Calculate();
}
//Remove an item
function Remove($a_ID)
{
global $sId , $sName , $sCount , $sPrice , $sDiscount ,
$sGoodPrice , $sMaxCount ;
$k = count($sId);
for($i=0; $i < $k; $i++)
{
If($sId[$i] == $a_ID)
{
$sCount[$i] = 0;
break;
}
}
$this->Update();
$this->Calculate();
}
//Change the number of items
function ModifyCount($a_i,$a_Count)
{
global $sCount;
$sCount[$a_i] = $a_Count ;
$this->Update();
$this->Calculate();
}
/*****************************
Clear all products
*******************************/
function RemoveAll()
{
session_unregister('sId');
Session_unregister('sName');
Session_unregister('sPrice');
Session_unregister('sDiscount');
session_unregister('sGoodPrice') ;
session_unregister('sCount') ;
Session_unregister('sMaxCount') ;
$this->SortCount = 0;
$this->TotalCost = 0;
}
//Whether a certain product is already in the blue, the parameter is the ID of this product
function Exists($a_ID)
{
for($i=0; $i<$this->SortCount; $i++)
{
If($this->Id[$i]==$a_ID) return TRUE;
}
Return FALSE;
}
//The position of a certain item in the blue
function IndexOf($a_ID)
{
for($i=0; $i<$this->SortCount; $i++)
{
If($this->Id[$i]==$id) return $i;
}
Return 0;
}
//Get information about a product, main working function
//Return an associative array,
function Item($i)
{
$Result[id] = $this->Id[$i];
$Result[name] = $this->Name[$i];
$Result[price] = $this->Price[$i];
$Result[count] = $this->Count[$i];
$Result[discount] = $this->Discount[$i] ;
$Result[goodprice] = $this->GoodPrice[$i] ;
$Result[maxcount] = $this->MaxCount[i];
Return $Result;
}
//Get the total number of product categories
function CartCount()
{
Return $this->SortCount;
}
//Get the total product value
function GetTotalCost()
{
Return $this->TotalCost;
}
}
?>
I hope this article will be helpful to everyone’s PHP programming design.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.
