Home >Backend Development >PHP Tutorial >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_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:12:331051browse

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.

Copy code The code is as follows:
/*****************************************************************************/
/*                                                                                                            /* 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/920616.htmlTechArticleThe relatively complete shopping cart class implemented by PHP, the php shopping cart class This article describes the relatively complete shopping cart class implemented by PHP Shopping cart category. Share it with everyone for your reference. The specific implementation method is as follows:...
Statement:
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