PHP Shopping Cart type, transplanted in CodeIgniter

Source: Internet
Author: User
Tags codeigniter
PHP Shopping Cart category, ported to CodeIgniter

 Session (' Cart_contents ')!== FALSE) {$this->_cart_contents = $this->session (' cart_contents ');} else{//initialization Data $this->_cart_contents[' cart_total '] = 0; $this->_cart_contents[' total_items '] = 0;}} --------------------------------/** * Added to Cart * * @accesspublic * @paramarray * @returnbool */function Insert ($items = a Rray ()) {//detect if the data is correct if (! Is_array ($items) or count ($items) = = 0) {return FALSE;} You can add a commodity (one-dimensional array), or you can add multiple items (two-dimensional arrays) $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;}}} Update data if ($save _cart = = True) {$this->_save_cart (); return TRUE;} return FALSE;} --------------------------------/** * Processing Insert cart Data * * @accessprivate * @paramarray * @returnbool */function _insert ($item s = Array ()) {//Check cart if (! Is_array ($items) OR count ($items) = = 0) {return FALSE;} --------------------------------/* First four array indexes (ID, Qty, price, and name) are required.   If either of these is missing, the data will not be saved in the shopping cart. The 5th Index (options) is optional.   When your product contains some information about the options, you can use it. Please use an array to save the option information.                Note: The value of $data [' price '] must be greater than 0 such as: $data = array (' id ' = ' = ' sku_123abc ', ' qty ' = = 1, ' Price ' = 39.95, ' name ' = ' T-shirt ', ' options ' = = Array (' Size ' = ' L ', ' Color ' = ' Red '); */if (! isset ($items [' id ']) or! isset ($items [' qty ']) or! isset ($items [' pri Ce ']) OR! Isset ($items [' name '])) {return FALSE;} --------------------------------//Quantity verification, not the number replaced by the null $items[' qty ' = Trim (Preg_replace ('/([^0-9])/I ', ', $items [' qty ' ]);//Quantity Verification $items[' qty ' = Trim (Preg_replace ('/(^[0]+)/I ', ' ', $items [' qty ']));//Quantity must be numeric or not 0if (! is_numeric ($items [' Qty ']) OR $items [' qty '] = = 0) {return FALSE;} --------------------------------//Product ID Verify if (! Preg_match ("/^[". $this->product_id_rules. "] +$/i ", $items [' ID '])) {return FALSE;} --------------------------------//Verify product name, considerTo kanji, do not use/*IF (! Preg_match ("/^[". $this->product_name_rules. "] +$/i ", $items [' name '])) {return FALSE;} *///--------------------------------//Price Verification $items[' prices ' = Trim (Preg_replace ('/([^0-9\.]) /I ', ' ', $items [' Price ']), $items [' prices '] = Trim (Preg_replace ('/(^[0]+)/I ', ' ', $items [')]);//Verify if the value is a numeric if (! is _numeric ($items [' price ']) {return FALSE;} --------------------------------//property validation, if the attribute exists, the attribute value + Product ID is encrypted to be saved in $rowid if (Isset ($items [' Options ']) and COUNT ($ items[' options ') > 0) {$rowid = MD5 ($items [' ID '].implode (', $items [' Options ']);} else{//the Product ID directly when there is no attribute $rowid = MD5 ($items [' id ']);} Detect whether the product in the shopping cart, if there is, on the original basis and add this new number of goods $_contents = $this->_cart_contents;$_tmp_contents = Array (); foreach ($_ Contents as $val) {if (Is_array ($val) and Isset ($val [' rowID ') and Isset ($val [' qty ']) and $val [' rowID ']== $rowid) {$_tmp_ contents[$val [' rowID ']][' qty '] = $val [' qty '];} else {$_tmp_contents[$val [' rowID ']][' qty '] = 0;}} --------------------------------//Clear the original data unset ($this->_cart_contents[$rowid]);//re-assign $this->_cart_contents[$rowid [' rowid '] = $rowid;//Add New project foreach ($items as $key + $val) {if ( $key = = ' Qty ' && isset ($_tmp_contents[$rowid [$key])) {$this->_cart_contents[$rowid] [$key] = $val +$_tmp_ contents[$rowid] [$key];} else {$this->_cart_contents[$rowid] [$key] = $val;}} return TRUE;} --------------------------------/** * Update cart * * @accesspublic * @paramarray * @paramstring * @returnbool */function up Date ($items = Array ()) {//verify if (! Is_array ($items) OR count ($items) = = 0) {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;}}} if ($save _cart = = True) {$this->_save_cart (); return TRUE;} return FALSE;} --------------------------------/** * Processing Update cart * * @accessprivate * @paramarray * @returnbool */function _update ($items = Array ()) {if (! isset ($items [' qty ']) or! isset ($items [' rowid ']) or! isset ($this->_cart_contents [$items [' rowID ']]) {return FALSE;} Number of detections $items[' qty ' = Preg_replace ('/([^0-9])/I ', ', $items [' qty ']), if (! is_numeric ($items [' qty ']) {return FALSE;} if ($this->_cart_contents[$items [' rowID ']][' qty '] = = $items [' qty ']) {return FALSE;} if ($items [' qty '] = = 0) {unset ($this->_cart_contents[$items [' rowid ']);} else{$this->_cart_contents[$items [' rowID ']][' qty '] = $items [' qty '];} return TRUE;} --------------------------------/** * Save cart to Session * @accessprivate * @returnbool */function _save_cart () {unset ($ this->_cart_contents[' Total_items '); unset ($this->_cart_contents[' cart_total '); $total = 0; $items = 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 ']; $items + = $val [' qty ']; $this->_cart_contents[$key] [' subtotal '] = ($this_cart_contents[$key [' Price '] * $this->_cart_contents[$key] [' qty ']);} $this->_cart_contents[' total_items ' = $items; $this->_cart_contents[' cart_total '] = $total; if (count ($this- >_cart_contents) <= 2) {$this->session (' cart_contents ', Array ()); return FALSE;} $this->session (' cart_contents ', $this->_cart_contents); return TRUE;} --------------------------------/** * Total amount in CART * * @accesspublic * @returninteger */function () {return $this-&gt ; _cart_contents[' cart_total ');}  --------------------------------/** * Total number of items in cart * * * @accesspublic * @returninteger */function Total_items () {return $this->_cart_contents[' Total_items ');} --------------------------------/** * An array of all information in the shopping cart * Returns an array containing all the information in the shopping cart * * @accesspublic * @returnarray */function Co Ntents () {$cart = $this->_cart_contents;unset ($cart [' total_items ']); unset ($cart [' cart_total ']); return $cart;} --------------------------------/** * Is there a specific column in the shopping cart containing the option information * * If a particular column in the shopping cart contains option information, this function returns TRUE (Boolean), this function is designed to be a COntents () together in a loop using * * @accesspublic * @returnarray */function has_options ($rowid = ") {if (! isset ($this->_cart_conte nts[$rowid] [' Options ']) OR count ($this->_cart_contents[$rowid [' options ']) = = = 0) {return FALSE;} return TRUE;} --------------------------------/** * Returns the option information for a particular item as an array * * This function is designed to be used with contents () in a loop * * @accesspublic * @returnarr Ay */function product_options ($rowid = ") {if (! isset ($this->_cart_contents[$rowid] [' options ']) {return array ()} return $this->_cart_contents[$rowid [' Options '];} --------------------------------/** * Formatted value * * Returns a value with a decimal point after formatting (2 digits after the decimal point), General price Use * * @accesspublic * @returninteger */funct Ion Format_number ($n = ") {if ($n = =") {return ';} $n = Trim (Preg_replace ('/([^0-9\.]) /I ', ', $n)); return Number_format ($n, 2, '. ', ', ');} --------------------------------/** * Destroy cart * This function is usually called after processing the user order * * @accesspublic * @returnnull */function Destroy () {unset ($this->_cart_contents); $this->_cart_contents[' cart_total '] = 0; $this->_cart_contents[' total_items '] = 0; $this->session (' cart_contents ', Array ());} --------------------------------/** * Save Session * * must have session_start (); * * @accessprivate * @returnbool */function session ($name = ' cart_contents ', $value = NULL) {if ($name = = ') $name = ' Cart_c Ontents '; if ($value = = NULL) {return @$_session[$name];} else {if (!empty ($value) && Is_array ($value)) {$_session [$name] = $value; return TRUE;} else {return FALSE;}}}? >
  • Related Article

    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.