The most important feature of a shopping cart is that it uses only one session

Source: Internet
Author: User
Tags foreach array count empty
session| Shopping Cart You can change according to their needs, the biggest feature is only a session, and some places are used in the previous online shopping cart class rewrite, I did not properly organized
<?php
Class Cart {

var $sortCount; Number of types of goods
var $totalCost; Total amount of merchandise
/* All products, such as: $myCart [5][$name]: Product number 5 name
* $myCart [5][$price]: Unit price of item number 5
* $myCart [5][$count]: Number of items number 5
* $myCart [5][$cost]: Total amount of item number 5
*/
var $myCart;
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 $Count; Number of pieces per item (array)
var $Cost; Value of each type of commodity (array)


Constructors
function cart () {
$this->sortcount = 0;
$this->totalcost = 0;
$this->mycart = Array ();
Session_Start (); Initializes a session
if (session_is_registered ("Mycart") ==false) session_register (' Mycart ');
$this->update ();
$this->calculate ();

}

Private, updating the corresponding data in the class based on the value of the session
function Update () {
Session_Start (); Initializes a session
$myCart = $_session["Mycart"];
if (false== $myCart)
{
$this->sortcount = 0;
$this->totalcost = 0;
$this->mycart = Array ();
return false;
}
Total quantity of goods obtained
$this->sortcount=count ($myCart);
if ($this->sortcount>0)
{
Start calculating the amount of a commodity
$totalCost = 0;
foreach ($myCart as $key => $val)
{
Rounding first
foreach ($val as $proName => $proVal)
{
if ($proName!= "name")
{
$val [$proName] = round (Eregi_replace (",", "", $proVal), 2);
$myCart [$key] [$proName] = $val [$proName];
}
}

Calculate the amount of each item
$myCart [$key] ["cost"] = Round ($val ["Count"]* $val ["Price"], 2);
Get the amount of all the goods
$totalCost + + $myCart [$key] ["cost"];
}
$this->totalcost = $totalCost;
$this->mycart = $myCart;
$_session["Mycart"] = $myCart;

}

}

/**
* Format numbers as currency data
*
*
**/
function Formatnum ($data)
{
foreach ($data as $key => $val)
{
foreach ($val as $sName => $sValue)
{
if ($sName!= "name")
{
$data [$key] [$sName] = Number_format ($sValue, 2);
}
}
}
return $data;

}
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 AddOne ($id, $na, $PR)
{
Session_Start (); Initializes a session
$myCart = $_session["Mycart"];
Set the number in the shopping cart
$myCart [$id] ["name"] = $na;
$myCart [$id] ["price"] = $PR;
+ + $myCart [$id] ["Count"];
$_session["Mycart"] = $myCart;
Update the member data for the class
$this->update ();

}
/**
* Add a set of items to the cart, if not, add it, if it already exists, update to data
* @param $data-The item to be added, in the form:
* $data [0][id], $data [0][name],
* $data [0][price], $data [0][count]
* @return Boolean
*/
function AddData ($data)
{
if (count ($data > 0))
{
Session_Start (); Initializes a session
$myCart = $_session["Mycart"];
foreach ($data as $val)
{
Extract ($val);
Set the number in the shopping cart
$myCart [$id] ["name"] = $name;
$myCart [$id] ["price"] = $price;
$myCart [$id] ["count"] = $count;
}
$_session["Mycart"] = $myCart;
Update the member data for the class
$this->update ();
}
}
/*
* Change the unit price of a product
*
*
*
**/
function Updateprice ($id, $price)
{
if ($price <=0) return false;
Session_Start (); Initializes a session
$myCart = $_session["Mycart"];
if ($myCart [$id]==true)
{
$myCart [$id] ["Price"]= $price;

$_session["Mycart"] = $myCart;
$this->update ();
}
}
Reduce the quantity of one item by 1
function Removeone ($id)
{
$count = $this->mycart[$id] ["Count"];
if ($count >0)
{
$this->modifycount ($id,-$count);
}

}

Change the number of items, if the unit price, change the unit price together
function Modifycount ($id, $ncount, $price =0)
{
if ($ncount <= 0) return false;
Session_Start (); Initializes a session
$myCart = $_session["Mycart"];
if ($myCart [$id]==true)
{
$myCart [$id] ["Count"]= $ncount;
If there is an incoming unit price, change the unit price together
if ($price >0) $myCart [$id] ["Price"]= $price;

$_session["Mycart"] = $myCart;
$this->update ();
}

}

Empty a product
function Emptyone ($i)
{
Session_Start (); Initializes a session
$myCart = $_session["Mycart"];
Unset ($myCart [$i]);
if (count ($myCart) ==0)
{
$this->emptyall ();
}else{
$_session["Mycart"] = $myCart;
$this->update ();
}
}


/***************************
Clear all the merchandise.

Because PHP does not support the Session_destroy () function in win, this empty function is not perfect,
Just set the number of each item to 0.
If you are under Linux, you can do it directly with Session_destroy ().
*****************************/
function Emptyall ()
{
Session_Start (); Initializes a session
$myCart = $_session["Mycart"];

Unset ($myCart);
$_session["Mycart"] = $myCart;
$this->update ();

}

/**
* Return all the data in the shopping cart
*
**/
function GetData ()
{
if ($this->sortcount > 0)
{
return $this->mycart;
}else{
return Array ();
}
}
Take a piece of merchandise information, the main work function
Returns an associative array with the subscript corresponding to the Id,name,price,count,cost
function GetOne ($i) {
$data = $this->mycart[$i];
if (false== $data) return array ();

$data ["id"] = $i;
return $data;

}

Total number of items taken
function Getsortcount () {
return $this->sortcount;
}

Take the total commodity value
function Gettotalcost () {
return $this->totalcost;
}

End Class
}

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.