Home>

Previous developers used js technology to develop a set of front-end shopping carts (deleted and added everything use js), but browser compatibility is not good, Finally something went wrong today, A foreigner bought a product, Due to the use of an unknown browser, chrome, opera… are possible, So I got an extra job, Rewrite the shopping cart.

Don't plan to use js anymore, consider php directly.

Found a shopping cart class, Easy to use.

cart.class.php Source:

<?php
/**
 * cart
 *
 * Shopping cart
 *
 * @author doodoo<[email protected]="">
 * @package cart
 * @category cart
 * @license php license
 * @access public
 * @version $revision:1.10 $
 * /
class cart {
 var $cart;
 var $totalcount;//Total number of products
 var $totalprices;//Total amount of the product
 /**
  * cart constructor
  *
  * The constructor of the class,Keep the shopping cart stable and initialized
  *
  * @static
  * @access public
  * @return void No return value
  * @param void No parameters
  * /
 function cart () {
 $this->totalcount=0;
 $this->totalprice=0;
 $this->cart=array ();
 }
 //}}}
 //{{{add ($item)
 /**
 * Add items to the current cart
 *
 * @access public
 * @param array $item product information (one-dimensional array:array (product id, product name,Commodity price,Number of Products))
 * @return array returns an array of items in the current cart
 * /
 function add ($item) {
 if (! is_array ($item) || is_null ($item)) return $this->cart;
 if (! is_numeric (end ($item)) || (! is_numeric (prev ($item)))) {
 echo "price and quantity must be numbers";
 return $this->cart;
 }
 reset ($item);//This sentence is required,Because the above judgment has moved the index of the array
 $key=current ($item);
 if ($key == "") return $this->cart;
 if ($this->_ isexists ($key)) {//Does the product already exist?
 $this->cart [$key] ["count"]=end ($item);
 return $this->cart;
 }
 $this->cart [$key] ["id"]=$key;
 $this->cart [$key] ["name"]=next ($item);
 $this->cart [$key] ["price"]=next ($item);
 $this->cart [$key] ["count"]=next ($item);
 return $this->cart;
 }
 //}}}
 //{{{add ($item)
 /**
 * Remove some or all items from the current cart
 * When $key == "", clear the current shopping cart
 * When $key!=""&& $count == "", pick out all products with the product id number from the current shopping cart
 * When $key!=""&& $count!="", Pick out $count items from the current shopping cart with item id $key
 *
 * @access public
 * @param string $key product id
 * @return mixed returns an array of real or fake items in the current cart
 * /
 function remove ($key="", $count="") {
 if ($key == "") {
 $this->cart=array ();
 return true;
 }
 if (! array_key_exists ($key, $this->cart)) return false;
 if ($count == "") {//Remove this category of goods
 unset ($this->cart [$key]);
 } else {//Remove $count items
 $this->cart [$key] ["count"]-= $count;
 if ($this->cart [$key] ["count"]<= 0) unset ($this->cart [$key]);
 }
 return $this->cart;
 }
 //}}}
 //{{{modi ($key, $value)
 /**
 * Modify the number of products with the item id $key in the shopping cart to $value
 *
 * @access public
 * @param string $key product id
 * @param int $value number of products
 * @return array returns an array of items in the current shopping cart;
 * /
 function modi ($key, $value) {
 if (! $this->_isexists ($key)) return $this->cart ();//This item does not exist,Return directly
 if ($value<= 0) {//value is too small, delete all
 unset ($this->cart [$key]);
 return $this->cart;
 }
 $this->cart [$key] ["count"]=$value;
 return $this->cart;
 }
 /**
 * Returns an array of items in the current cart
 *
 * @access public
 * @return array returns an array of items in the current shopping cart;
 * /
 function getcart () {
 return $this->cart;
 }
 //}}}
 //{{{_isexists ($key)
 /**
 * Determine if there is a product with the item id $key in the current shopping cart
 *
 * @access private
 * @param string $key product id
 * @return bool true or false;
 * /
 function _isexists ($key)
 {
 if (isset ($this->cart [$key])&&! empty ($this->cart [$key])&&array_key_exists ($key, $this->cart))
 return true;
 return false;
 }
 //}}}
 //{{{isempty ()
 /**
 * Determine if the current shopping cart is empty,I.e. no products
 *
 * @access public
 * @return bool true or false;
 * /
 function isempty () {
 return! count ($this->cart);
 }
 //}}}
 //{{{_stat ()
 /**
 * Get some statistics
 *
 * @access private
 * @return bool true or false;
 * /
 function _stat () {
 if ($this->isempty ()) return false;
 foreach ($this->cart as $item) {
 $this->[email protected] ($item);
 $this->[email protected] ($item);
 }
 return true;
 }
 //}}}
 //{{{totalprices ()
 /**
 * Get the total amount of all items in the current shopping cart
 *
 * @access public
 * @return float return amount;
 * /
 function totalprices () {
 if ($this->_ stat ())
 return $this->totalprices;
 return 0;
 }
 //}}}
 //{{{isempty ()
 /**
 * Get the total number of all items in the current shopping cart and
 *
 * @access public
 * @return int;
 * /
 function totalcount () {
 if ($this->_ stat ())
 return $this->totalcount;
 return 0;
 }
} //end class cart
?>

Methods using this class:

<?php
header ("content-type:text/html;charset=utf8");
//Call instance
require_once "cart.class.php";
session_start ();
if (! isset ($_ session ["cart"])) {
 $_session ["cart"]=new cart;
}
$cart =&$_session ["cart"];
if (($_server ["request_method"] == "post")&&($_ post ["action"] == "add")) {
 $p=$_post ["p"];
 $items=$cart->add ($p);
}
if (($_get ["action"] == "remove")&&($_ get ["key"]!="")) {
 $items=$cart->remove ($_ get ["key"]);
}
if (($_server ["request_method"] == "post")&&($_ post ["action"] == "modi")) {
 $key=$_post ["key"];
 $value=$_post ["value"];
 for ($i=0;$i<count ($key);$i="" $items="$cart-") {= "">modi ($key [$i], $value [$i ]);
 }
}
$items=$cart->getcart ();
//print
echo "";
setlocale (lc_monetary, "it_it");
foreach ($items as $item) {
 echo "";
 echo "&table border =" 1 "><tbody><tr><form action =" \"index.php \""method =" \ "post \?? ;= ""></form><td&id;id:". $Item [" id "]."&Input type="hidden" value=". $Item [" id "]." Name="key []">"echo"</td><td>Product:". $item [" name "];echo"</td>["price"];echo "</td><td><input value =". $item ["count"]. "name =" value [] ">";$sum=$item [ "count"] * $item ["price"];echo "</td><td>Total:" .round ($sum, 2);echo "&/td>onclick="\"location ="?action=remove&key =". $item ["id"].""\"" type="button" value="Remove">";} echo "<input type =" hidden "value =" modi "name =" action ">";echo "</td></tr><tr><td colspan =" 7 "><input type="submit" value="Submit query content"""echo"/td>&l t;/tr></tbody></table>";
?>
<hr>
<form action="tmp.php" method="post">
id:<input name="p []">
Product Name:<input name="p []">
Price per unit:<input name="p []">
Quantity:<input name="p []">
<input type="hidden" value="add" name="action">
<input type="submit" value="submit query content">
</form></count>
php
  • Previous Using Python to implement book overdue reminders
  • Next Detailed use of MvcPager for MVC paging