This article describes the implementation of a more comprehensive PHP shopping cart class. Share to everyone for your reference. The implementation methods are as follows:
To do a project a long time ago, you need to use a shopping cart, considering that it may be used frequently, so encapsulate it into a class so that it can be invoked later, and interested readers could simply modify the class to use it in their own program.
Copy Code code as follows:
<?php
/*****************************************************************************/
/* */
/* File type: Include file, recommended suffix is. Inc *
/* */
/* File Name:cart.inc * *
/* */
/* Description: Define a car-buying category * *
/* */
/* Func list:class cart * *
/* */
* Author:bigeagle * *
/* */
/* */
/*****************************************************************************/
Define this file constant
Define ("_cart_inc_", "exists");
/* Shopping Cart class * *
Class Tcart
{
var $SortCount; Number of types of goods
var $TotalCost; Total value of goods
var $Id; ID (array) for each category of goods
var $Name; Name of each type of commodity (array)
var $Price; Price of each type of commodity (array)
var $Discount; Discount for merchandise (array)
var $GoodPrice; Preferential prices for goods (array)
var $Count; Number of pieces per item (array)
var $MaxCount; Product Limits (Array)
Constructors
function Tcart ()
{
$this->sortcount=0;
Session_Start (); Initializes a 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 ();
}
Private, updating the corresponding data in the class based on the value of the 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;
Calculate the total number of items
$this->sortcount=count ($sId);
}
Private, calculating the value of each commodity and the total price of all goods according to the new data
function Calculate ()
{
For ($i =0 $i < $this->sortcount; $i + +)
{
/* Calculate the value of each item, if the discount is 0, then for the preferential price * *
$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 a piece of merchandise
Judge whether there is already in the blue, if so, add count, otherwise add a new product
The first is to change the value of the session, and then call Update () and calculate () to update the member variable
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 + +)
{//Find out if you have already joined this product
if ($sId [$i]== $a _id)
{
$sCount [$i] + = $a _count;
Break
}
}
if ($i >= $k)
{//No, 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 for the class
$this->calculate ();
}
Remove a piece of merchandise
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 the merchandise.
*****************************/
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;
}
If a product is already in the blue, the parameter is the ID of the 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 product in the blue
function IndexOf ($a _id)
{
For ($i =0 $i < $this->sortcount; $i + +)
{
if ($this->id[$i]== $id) return $i;
}
return 0;
}
Take a piece of merchandise information, the main work function
Returns 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;
}
Total number of items taken
function Cartcount ()
{
return $this->sortcount;
}
Take the total commodity value
function Gettotalcost ()
{
return $this->totalcost;
}
}
?>
I hope this article will help you with your PHP program design.