码迷,mamicode.com
首页 > 其他好文 > 详细

opencart中的购物车类

时间:2016-05-17 11:30:07      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:

 

数据库的用户表customer中设置了一个cart字段,类型是text,这个字段保存当前用户的购物车信息。

+-------------------+--------------+------+-----+---------+----------------+
| 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    |                |
+-------------------+--------------+------+-----+---------+----------------+

 

添加商品到购物车:

public function add($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;
            }
        }
}

 

当用户登录时,从表中取出对应的cart内容,保存到session中,这样可以在程序的任何位置都能访问到

public function login($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) {//查询的结果是否存在
            $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;
                    }
                }
            }
    }
}

 

system/library/customer.php的构造函数中:

 

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();
            }
        }
    }

这样就可以保持实时的把购物车的内容同步到表中。

 

opencart中的购物车类

标签:

原文地址:http://www.cnblogs.com/tonety/p/5500407.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!