The user table for the database has a CART field set in customer, and the type is text, which saves the current user's shopping cart information.
+-------------------+--------------+------+-----+---------+----------------+| Field | Type |Null|Key|Default| Extra |+-------------------+--------------+------+-----+---------+----------------+| customer_id | Int (11) | NO | PRI |NULL| auto_increment | | customer_group_id | Int (11) | NO | |NULL| || store_id | Int (11) | NO | | 0 | || name | varchar (32) | NO | |NULL| || email | VARCHAR (96) | NO | |NULL| || Telephone | varchar (32) | NO | |NULL| || password | varchar (40) | NO | |NULL| || Salt | varchar (9) | NO | |NULL| || Cart | Text | YES | |NULL| || Wishlist | Text | YES | |NULL| || Newsletter | tinyint (1) | NO | | 0 | || address_id | Int (11) | NO | | 0 | || Custom_field | Text | NO | |NULL| || IP | varchar (40) | NO | |NULL| || Status | tinyint (1) | NO | |NULL| || Approved | tinyint (1) | NO | |NULL| || safe | tinyint (1) | NO | |NULL| || Token | varchar (255) | NO | |NULL| || date_added | datetime | NO | |NULL| |+-------------------+--------------+------+-----+---------+----------------+
Add Item to Cart:
Public functionAdd$product _id,$qty= 1,$option=Array(),$recurring _id= 0) { $this->data =Array(); $product[' product_id '] = (int)$product _id; if($option) { $product[' option '] =$option; } if($recurring _id) { $product[' recurring_id '] = (int)$recurring _id; } $key=Base64_encode(Serialize($product)); if((int)$qty&& ((int)$qty> 0)) { if(!isset($this->session->data[' cart ' [$key])) { $this->session->data[' cart ' [$key] = (int)$qty; } Else { $this->session->data[' cart ' [$key] + = (int)$qty; } }}
When the user logs in, the corresponding cart content is removed from the table and saved to the session so that it can be accessed anywhere in the program
Public functionLogin$email,$password,$override=false) { $customer _query=$this->db->query ("SELECT * from".) Db_prefix. "Customer WHERE LOWER (email) = '".$this->db->escape (Utf8_strtolower ($email)) . "' and (password = SHA1 (CONCAT (Salt, SHA1 (CONCAT (Salt, SHA1 (').$this->db->escape ($password) . "')))) OR password = '".$this->db->escape (MD5($password)) . "') and status = ' 1 ' and approved = ' 1 '"); if($customer _query-num_rows) {//The result of the query is present$this->session->data[' customer_id '] =$customer _query->row[' customer_id ']; if($customer _query->row[' cart ' &&is_string($customer _query->row[' cart '])) { $cart=unserialize($customer _query->row[' cart ']); foreach($cart as $key=$value) { if(!array_key_exists($key,$this->session->data[' cart '])) { $this->session->data[' cart ' [$key] =$value; } Else { $this->session->data[' cart ' [$key] +=$value; } } } }
}
In the system/library/customer.php constructor:
Public function__construct ($registry) { $this->config =$registry->get (' config '); $this->db =$registry->get (' DB '); $this->request =$registry->get (' request ')); $this->session =$registry->get (' Session ')); if(isset($this->session->data[' customer_id '])) { $customer _query=$this->db->query ("SELECT * from".) Db_prefix. "Customer WHERE customer_id = '". (int)$this->session->data[' customer_id '. "' and status = ' 1 '"); if($customer _query-num_rows) { $this->customer_id =$customer _query->row[' customer_id ']; $this->db->query ("UPDATE". Db_prefix. "Customer SET cart = '".$this->db->escape (isset($this->session->data[' cart ')?Serialize($this->session->data[' cart '): '). "', wishlist = '".$this->db->escape (isset($this->session->data[' wishlist ')?Serialize($this->session->data[' wishlist '): '). "', IP = '".$this->db->escape ($this->request->server[' REMOTE_ADDR '). "' WHERE customer_id = '". (int)$this->customer_id. "‘"); } Else { $this-logout (); } } }
In this way, you can keep the contents of your shopping cart in real-time in sync with the table.
Shopping Cart class in OpenCart