Home >Backend Development >PHP Tutorial >Example of shopping cart class implemented in PHP_PHP tutorial
This article describes the example of shopping cart class implemented in PHP. Share it with everyone for your reference. The specific analysis is as follows:
This shopping cart class is implemented based on CodeIgniter’s shopping cart class imitation.
The basic functions of the shopping cart are as follows:
1) Add items to shopping cart
2) Delete items from shopping cart
3) Update shopping cart item information [1/-1]
4) Statistics of shopping cart items
1. Total Project
2. Total quantity
3. Total amount
5) Calculate the quantity and amount of individual shopping items
6) Clear shopping cart
1. cart.php file:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
/** * * @author quanshuidingdang */ class Cart { //物品id及名称规则,调试信息控制 private $product_id_rule = '.a-z0-9-_'; //小写字母 | 数字 | ._- private $product_name_rule = '.:a-z0-9-_';//小写字母 | 数字 | ._-: private $debug = TRUE; //购物车 private $_cart_contents = array(); /** * Constructor * * @param array */ public function __construct() { //是否第一次使用? if(isset($_SESSION['cart_contents'])) { $this->_cart_contents = $_SESSION['cart_contents']; } else { $this->_cart_contents['cart_total'] = 0; $this->_cart_contents['total_items'] = 0; } if($this->debug === TRUE) { //$this->_log("cart_create_success"); } } /** * Add items to cart * * @access public * @param array One-dimensional or multi-dimensional array, must contain key value name: id -> Item ID, qty -> quantity, price -> unit price (price), name -> Item name * @return bool */ public function insert($items = array()) { //输入物品参数异常 if( ! is_array($items) OR count($items) == 0) { if($this->debug === TRUE) { $this->_log("cart_no_items_insert"); } return FALSE; } //物品参数处理 $save_cart = FALSE; if(isset($items['id'])) { if($this->_insert($items) === TRUE) { $save_cart = TRUE; } } else { foreach($items as $val) { if(is_array($val) AND isset($val['id'])) { if($this->_insert($val) == TRUE) { $save_cart = TRUE; } } } } //当插入成功后保存数据到session if($save_cart) { $this->_save_cart(); return TRUE; } return FALSE; } /** * Update shopping cart item information * * @access public * @param array * @return bool */ public function update($items = array()) { //输入物品参数异常 if( !is_array($items) OR count($items) == 0) { if($this->debug === TRUE) { $this->_log("cart_no_items_insert"); } return FALSE; } //物品参数处理 $save_cart = FALSE; if(isset($items['rowid']) AND isset($items['qty'])) { if($this->_update($items) === TRUE) { $save_cart = TRUE; } } else { foreach($items as $val) { if(is_array($val) AND isset($val['rowid']) AND isset($val['qty'])) { if($this->_update($val) === TRUE) { $save_cart = TRUE; } } } } //当更新成功后保存数据到session if($save_cart) { $this->_save_cart(); return TRUE; } return FALSE; } /** * Get the total amount of items in the shopping cart * * @return int */ public function total() { return $this->_cart_contents['cart_total']; } /** * Get shopping cart item type * * @return int */ public function total_items() { return $this->_cart_contents['total_items']; } /** * Get shopping cart * * @return array */ public function contents() { return $this->_cart_contents; } /** * Get shopping cart item options * * @param string * @return array */ public function options($rowid = '') { if($this->has_options($rowid)) { return $this->_cart_contents[$rowid]['options']; } else { return array(); } } /** * Clear shopping cart * */ public function destroy() { unset($this->_cart_contents); $this->_cart_contents['cart_total'] = 0; $this->_cart_contents['total_items'] = 0; unset($_SESSION['cart_contents']); } /** * Determine whether the shopping cart items have options * * @param string * @return bool */ private function has_options($rowid = '') { if( ! isset($this->_cart_contents[$rowid]['options']) OR count($this->_cart_contents[$rowid]['options']) === 0) { return FALSE; } return TRUE; } /** * 插入数据 * * @access private * @param array * @return bool */ private function _insert($items = array()) { //Abnormal input item parameters if( ! is_array($items) OR count($items) == 0) { if($this->debug === TRUE) { $this->_log("cart_no_data_insert"); } return FALSE; } //If the item parameters are invalid (no id/qty/price/name) if( ! isset($items['id']) OR ! isset($items['qty']) OR ! isset($items['price']) OR ! isset($items['name' ])) { if($this->debug === TRUE) { $this->_log("cart_items_data_invalid"); } return FALSE; } //Remove left zero and non-numeric characters from item quantity $items['qty'] = trim(preg_replace('/([^0-9])/i', '', $items['qty'])); $items['qty'] = trim(preg_replace('/^([0] )/i', '', $items['qty'])); //If the item quantity is 0, or non-number, we will not do anything with the shopping cart! if( ! is_numeric($items['qty']) OR $items['qty'] == 0) { if($this->debug === TRUE) { $this->_log("cart_items_data(qty)_invalid"); } return FALSE; } //Item ID regular judgment if( ! preg_match('/^['.$this->product_id_rule.'] $/i', $items['id'])) { if($this->debug === TRUE) { $this->_log("cart_items_data(id)_invalid"); } return FALSE; } //Regular judgment of item name if( ! preg_match('/^['.$this->product_name_rule.'] $/i', $items['name'])) { if($this->debug === TRUE) { $this->_log("cart_items_data(name)_invalid"); } return FALSE; } //Remove the left zero and non-numeric (with decimal point) characters from the unit price of the item $items['price'] = trim(preg_replace('/([^0-9.])/i', '', $items['price'])); $items['price'] = trim(preg_replace('/^([0] )/i', '', $items['price'])); //If the unit price of the item is not a number if( ! is_numeric($items['price'])) { if($this->debug === TRUE) { $this->_log("cart_items_data(price)_invalid"); } return FALSE; } //Generate the unique id of the item if(isset($items['options']) AND count($items['options']) >0) { $rowid = md5($items['id'].implode('', $items['options'])); } else { $rowid = md5($items['id']); } //Add items to shopping cart unset($this->_cart_contents[$rowid]); $this->_cart_contents[$rowid]['rowid'] = $rowid; foreach($items as $key => $val) { $this->_cart_contents[$rowid][$key] = $val; } return TRUE; } /** * Update shopping cart item information (private) * * @access private * @param array * @return bool */ private function _update($items = array()) { //Abnormal input item parameters if( ! isset($items['rowid']) OR ! isset($items['qty']) OR ! isset($this->_cart_contents[$items['rowid']])) { if($this->debug == TRUE) { $this->_log("cart_items_data_invalid"); } return FALSE; } //Remove left zero and non-numeric characters from item quantity $items['qty'] = preg_replace('/([^0-9])/i', '', $items['qty']); $items['qty'] = preg_replace('/^([0] )/i', '', $items['qty']); //If the item quantity is non-numeric, no processing will be done to the shopping cart! if( ! is_numeric($items['qty'])) { if($this->debug === TRUE) { $this->_log("cart_items_data(qty)_invalid"); } return FALSE; } //If the number of items in the shopping cart is consistent with the number of items that need to be updated, no update is required if($this->_cart_contents[$items['rowid']]['qty'] == $items['qty']) { if($this->debug === TRUE) { $this->_log("cart_items_data(qty)_equal"); } return FALSE; } //If the number of items that need to be updated is equal to 0, it means that this item is not needed and should be removed from the shopping cart // Otherwise, modify the number of items in the shopping cart to be equal to the number of items entered if($items['qty'] == 0) { unset($this->_cart_contents[$items['rowid']]); } else { $this->_cart_contents[$items['rowid']]['qty'] = $items['qty']; } return TRUE; } /** * Save shopping cart data to session * * @access private * @return bool */ private function _save_cart() { //First clear the total item types and total amount in the shopping cart unset($this->_cart_contents['total_items']); unset($this->_cart_contents['cart_total']); //Then traverse the array to count the item types and total amount $total = 0; foreach($this->_cart_contents as $key => $val) { if( ! is_array($val) OR ! isset($val['price']) OR ! isset($val['qty'])) { continue; } $total = ($val['price'] * $val['qty']); //The total amount of each item $this->_cart_contents[$key]['subtotal'] = ($val['price'] * $val['qty']); } //Set the total item types and total amount in the shopping cart $this->_cart_contents['total_items'] = count($this->_cart_contents); $this->_cart_contents['cart_total'] = $total; //If the number of elements in the shopping cart is less than or equal to 2, the shopping cart is empty if(count($this->_cart_contents) <= 2) { unset($_SESSION['cart_contents']); return FALSE; } //Save shopping cart data to session $_SESSION['cart_contents'] = $this->_cart_contents; return TRUE; } /** * Logging * * @access private * @param string * @return bool */ private function _log($msg) { return @file_put_contents('cart_err.log', $msg, FILE_APPEND); } } /*End of file cart.php*/ /*Location /htdocs/cart.php*/ |
2. The cart_demo.php file is as follows:
?
3 45 12 13 |
<🎜>session_start();<🎜> <🎜>require_once('cart.php');<🎜> <🎜>$items = array(<🎜> <🎜>0 => array(<🎜> <🎜>'id' => 'sp001',<🎜> <🎜>'qty' => 20,<🎜> <🎜>'price' => '10.50',<🎜> <🎜>'name' => 'a002',<🎜> <🎜>'options' => array(<🎜> <🎜>'made' => 'china',<🎜> <🎜>'company' => 'bgi'<🎜> <🎜>)<🎜> <🎜>),<🎜> <🎜>1 => array(<🎜> <🎜>'id' => 'sp002',<🎜> <🎜>'qty' => 1,<🎜> <🎜>'price' => '3.50',<🎜> <🎜>'name' => 'b002'<🎜> <🎜>)<🎜> <🎜>);<🎜> <🎜>$arr = array(<🎜> <🎜>'rowid' => '86dbb7cb58a667558b4bbb1f60330028',<🎜> <🎜>'qty' => 21<🎜> <🎜>);<🎜> <🎜>$cart = new Cart();<🎜> <🎜>$cart->insert($items); //var_dump($cart->contents()); $cart->update($arr); var_dump($cart->contents()); //$cart->destroy(); //var_dump($_SESSION['cart_contents']); /*end of php*/ |