十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
這期內容當中小編將會給大家?guī)碛嘘P如何使用PHP實現(xiàn)購物車類Cart.class.php,文章內容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

創(chuàng)新互聯(lián)建站堅信:善待客戶,將會成為終身客戶。我們能堅持多年,是因為我們一直可值得信賴。我們從不忽悠初訪客戶,我們用心做好本職工作,不忘初心,方得始終。10余年網(wǎng)站建設經(jīng)驗創(chuàng)新互聯(lián)建站是成都老牌網(wǎng)站營銷服務商,為您提供成都網(wǎng)站制作、網(wǎng)站建設、網(wǎng)站設計、HTML5建站、網(wǎng)站制作、成都品牌網(wǎng)站建設、小程序定制開發(fā)服務,給眾多知名企業(yè)提供過好品質的建站服務。
之前的開發(fā)人員使用了JS的技術開發(fā)了一套前臺購物車(刪除添加什么的都使用JS),但是瀏覽器兼容不好, 今天終于出問題了, 有個老外購物了產(chǎn)品, 由于使用了不知名的瀏覽器, chrome, opera…都有可能, 因此, 我多了一份工作, 重寫購物車.
不打算再使用JS, 直接考慮php.
找到了一個購物車的類, 使用起來很方便.
Cart.class.php源碼:
* @package Cart
* @category Cart
* @license PHP License
* @access public
* @version $Revision: 1.10 $
*/
Class Cart{
var $cart;
var $totalCount; //商品總數(shù)量
var $totalPrices; //商品總金額
/**
* Cart Constructor
*
* 類的構造函數(shù),使購物車保持穩(wěn)定的初始化狀態(tài)
*
* @static
* @access public
* @return void 無返回值
* @param void 無參數(shù)
*/
function Cart(){
$this->totalCount = 0;
$this->totalPrice = 0;
$this->cart = array();
}
// }}}
// {{{ add($item)
/**
* 增加商品到當前購物車
*
* @access public
* @param array $item 商品信息(一維數(shù)組:array(商品ID,商品名稱,商品單價,商品數(shù)量))
* @return array 返回當前購物車內商品的數(shù)組
*/
function add($item){
if(!is_array($item)||is_null($item)) return $this->cart;
if(!is_numeric(end($item))||(!is_numeric(prev($item)))) {
echo "價格和數(shù)量必須是數(shù)字";
return $this->cart;
}
reset($item); //這一句是必須的,因為上面的判斷已經(jīng)移動了數(shù)組的指標
$key = current($item);
if($key=="") return $this->cart;
if($this->_isExists($key)){ //商品是否已經(jīng)存在?
$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)
/**
* 從當前購物車中取出部分或全部商品
* 當 $key=="" 的時候,清空當前購物車
* 當 $key!=""&&$count=="" 的時候,從當前購物車中揀出商品ID號為 $key 的全部商品
* 當 $key!=""&&$count!="" 的時候,從當前購物車中揀出 $count個 商品ID號為 $key 的商品
*
* @access public
* @param string $key 商品ID
* @return mixed 返回真假或當前購物車內商品的數(shù)組
*/
function remove($key="",$count=""){
if($key=="") {
$this->cart = array();
return true;
}
if(!array_key_exists($key,$this->cart)) return false;
if($count==""){ //移去這一類商品
unset($this->cart[$key]);
}else{ //移去$count個商品
$this->cart[$key]['count'] -= $count;
if($this->cart[$key]['count']<=0) unset($this->cart[$key]);
}
return $this->cart;
}
// }}}
// {{{ modi($key,$value)
/**
* 修改購物車內商品ID為 $key 的商品的數(shù)量為 $value
*
* @access public
* @param string $key 商品ID
* @param int $value 商品數(shù)量
* @return array 返回當前購物車內商品的數(shù)組;
*/
function modi($key,$value){
if(!$this->_isExists($key)) return $this->cart(); //不存在此商品,直接返回
if($value<=0){ // value 太小,全部刪除
unset($this->cart[$key]);
return $this->cart;
}
$this->cart[$key]['count'] = $value;
return $this->cart;
}
/**
* 返回當前購物車內商品的數(shù)組
*
* @access public
* @return array 返回當前購物車內商品的數(shù)組;
*/
function getCart(){
return $this->cart;
}
// }}}
// {{{ _isExists($key)
/**
* 判斷當前購物車中是否存在商品ID號為$key的商品
*
* @access private
* @param string $key 商品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()
/**
* 判斷當前購物車是否為空,即沒有任何商品
*
* @access public
* @return bool true or false;
*/
function isEmpty(){
return !count($this->cart);
}
// }}}
// {{{ _stat()
/**
* 取得部分統(tǒng)計信息
*
* @access private
* @return bool true or false;
*/
function _stat(){
if($this->isEmpty()) return false;
foreach($this->cart as $item){
$this->totalCount = @end($item);
$this->totalPrices = @prev($item);
}
return true;
}
// }}}
// {{{ totalPrices()
/**
* 取得當前購物車所有商品的總金額
*
* @access public
* @return float 返回金額;
*/
function totalPrices(){
if($this->_stat())
return $this->totalPrices;
return 0;
}
// }}}
// {{{ isEmpty()
/**
* 取得當前購物車所有商品的總數(shù)量和
*
* @access public
* @return int ;
*/
function totalCount(){
if($this->_stat())
return $this->totalCount;
return 0;
}
}//End Class 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;$imodi($key[$i],$value[$i]);
}
}
$items = $cart->getCart();
//打印
echo "";
setlocale(LC_MONETARY, 'it_IT');
foreach($items as $item){
echo "";
echo "ID:".$item['ID'].""; echo " 產(chǎn)品:".$item['name']; echo " 單價:".$item['price']; echo " "; $sum = $item['count']*$item['price']; echo " 合計:".round($sum,2); echo " "; } echo ""; echo " "; echo "
";
?>
ID:
品名:
單價:
數(shù)量:
上述就是小編為大家分享的如何使用PHP實現(xiàn)購物車類Cart.class.php了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。