opencart中的购物车类

 

数据库的用户表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();
            }
        }
    }

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

 

转载于:https://www.cnblogs.com/tonety/p/5500407.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值