The php shopping cart is used on e-commerce websites. It is like a supermarket shopping cart. After selecting the products, you first put them in your shopping cart and wait for them to settle at the counter. This php shopping cart completely follows This principle is used as an example. Let’s take a look below and use cookies to implement it.
The php tutorial shopping cart is used on e-commerce websites. It is like a supermarket shopping cart. After selecting the products, you first put them in your own shopping cart and then wait for them to settle at the counter. This php The shopping cart is completely implemented according to this principle. Let's take a look below and use cookies to implement it.
/**
* Shopping cart cookies are saved, and the storage period is 1 day. Note: the browser must support cookies to be able to use it
* Technical exchange group: 100352308
*/
class cartapi {
private $cartarray = array(); // Two-dimensional array to store the shopping cart
private $cartcount; // Count the number of shopping carts
public $expires = 86400; // Cookie expiration time, if it is 0, it will not be saved to the local unit. The unit is seconds
/**
* Constructor initialization operation If $id is not empty, add it directly to the shopping cart
*
*/
public function __construct($id = "",$name = "",$price1 = "",$price2 = "",$price3 = "",$count = "",$image = "",$expires = 86400 ) {
if ($id != "" && is_numeric($id)) {
$this->expires = $expires;
$this->addcart($id,$name, $price1,$price2,$price3,$count,$image);
}
}
/**
* Add product to shopping cart
*
* @param int $id product number
* @param string $name product name
* @param decimal $price1 product price
* @param decimal $price2 Product price
* @param decimal $price3 Product price
* @param int $count Product quantity
* @param string $image Product image
* @return if the product exists , then add 1 to the original quantity and return false
*/
public function addcart($id,$name,$price1 ,$price2,$price3,$count,$image) {
$this->cartarray = $this->cartview(); // Read and write data into the array
if ($this ->checkitem($id)) { // Check if the product exists
$this->modifycart($id,$count,0); // Add $count to the quantity of the product
return false;
}
$this->cartarray[0][$id] = $id;
$this->cartarray[1][$id] = $name;
$this-> cartarray[2][$id] = $price1;
$this->cartarray[3][$id] = $price2;
$this->cartarray[4][$id] = $ price3;
$this->cartarray[5][$id] = $count;
$this->cartarray[6][$id] = $image;
$this-> save();
}
/**
* Modify the items in the shopping cart
*
* @param int $id item number
* @param int $count item quantity
* @param int $flag Modification type 0: Add 1: Subtract 2: Modify 3: Clear
* @return If the modification fails, return false
*/
public function modifycart($id, $count, $flag = "") {
$tmpid = $id;
$this->cartarray = $this->cartview(); // Read and write data into the array
$tmparray = &$this->cartarray; // Reference
if (! is_array($tmparray[0])) return false;
if ($id < 1) {
return false;
}
foreach ($tmparray[0] as $item) {
if ($item === $tmpid) {
switch ($flag) {
case 0: // Generally, $count is 1
$tmparray[5][$id] + = $count;
break;
case 1: // Reduce the quantity
$tmparray[5][$id] -= $count;
break;
case 2: // Modify Quantity
if ($count == 0) {
unset($tmparray[0][$id]);
unset($tmparray[1][$id]);
unset( $tmparray[2][$id]);
unset($tmparray[3][$id]);
unset($tmparray[4][$id]);
unset($tmparray [5][$id]);
count;
break ;
}
case 3: // Clear items
unset($tmparray[0][$id]);
unset($tmparray[1][$id ]);
unset($tmparray[2][$id]);
unset($tmparray[3][$id]);
unset($tmparray[4][$id]);
unset($tmparray[5][$id]);
unset($tmparray[6][$id]);
break;
default:
break;
}
}
}
$this->save();
}
/**
* Clear shopping cart
*
*/
public function removeall() {
$this->cartarray = array();
$this->save();
}
/**
* View shopping cart information
*
* @return array Returns a two-dimensional array
*/
public function cartview() {
$cookie = strips教程lashes($_cookie['cartapi']);
if (!$cookie) return false;
$tmpunserialize = unserialize($cookie);
return $tmpunserialize;
}
/**
* Check if there are products in the shopping cart
*
* @return bool If there are products, return true, otherwise false
*/
public function checkcart() {
$tmparray = $this->cartview();
if (count($tmparray[0]) < 1) {
return false;
}
return true;
}
/**
* Product statistics
*
* @return array Returns a one-dimensional array $arr[0]: total price of product 1 $arr[1: total price of product 2 $arr[2]: product Total price of 3$arr[3]:Total quantity of product
*/
public function countprice() {
$tmparray = $this->cartarray = $this->cartview();
$outarray = array(); //一维数组
// 0 是产品1的总价格
// 1 是产品2的总价格
// 2 是产品3的总价格
// 3 是产品的总数量
$i = 0;
if (is_array($tmparray[0])) {
foreach ($tmparray[0] as $key=>$val) {
$outarray[0] += $tmparray[2][$key] * $tmparray[5][$key];
$outarray[1] += $tmparray[3][$key] * $tmparray[5][$key];
$outarray[2] += $tmparray[4][$key] * $tmparray[5][$key];
$outarray[3] += $tmparray[5][$key];
$i++;
}
}
return $outarray;
}
/**
* Count the number of products
*
* @return int
*/
public function cartcount() {
$tmparray = $this->cartview();
$tmpcount = count($tmparray[0]);
$this->cartcount = $tmpcount;
return $tmpcount;
}
/**
* Save the product If you do not use the construction method, this method must use
*
*/
public function save() {
$tmparray = $this->cartarray;
$tmpserialize = serialize($tmparray);
setcookie("cartapi",$tmpserialize,time()+$this->expires);
}
/**
* Check if the shopping cart item exists
*
* @param int $id
* @return bool if true if it exists otherwise false
*/
private function checkitem($id) {
$tmparray = $this->cartarray;
if (!is_array($tmparray[0])) return;
foreach ($tmparray[0] as $item) {
if ($item === $id) return true;
}
return false;
}
}
?>