public static function count_cart($user_id) { $count = array('total_fee' => 0, 'total_nums' => 0); $rows = \Db\Trade\Cart::fetch(array('user_id' => $user_id, 'cart_status' => 1)); foreach ($rows as $row) { $count['total_fee'] += $row->goods_nums * $row->goods_discount_price; $count['total_nums'] += $row->goods_nums; } return $count; }
/** * 删除商品 */ public function delete() { $ids = \Core\URI::kv('ids'); $v = new \Model\Validation(); $v->required($ids)->message('参数错误', 1000); if (!$v->has_error()) { $id_arr = explode('-', $ids); foreach ($id_arr as $id) { $model = \DB\Trade\Cart::row(array('user_id' => $this->user->user_id, 'goods_id' => $id)); if ($model) { $model->delete(); } } $v->set_data(\Db\Trade\Cart::count_cart($this->user->user_id)); } $v->send(); }
¥<em><?php echo $row->goods_discount_price; ?> </em> <del> 原价:<?php echo $row->goods_original_price; ?> </del> </p> </div> </a> <div class="index_count"> <div class="goods_count"> <div class="count"> <a href="javascript:;" class="dec"><div></div></a> <?php $init_nums = \Db\Trade\Cart::row(array('user_id' => $this->user->user_id, 'goods_id' => $row->goods_id)); ?> <input type="text" name="nums" value="<?php echo !empty($init_nums->goods_nums) ? $init_nums->goods_nums : 0; ?> "> <a href="javascript:;" class="inc"><div></div></a> </div> </div> <div class="goods_title"> <a href="<?php echo \Core\URI::a2p(array('main' => 'get', 'id' => $row->goods_id)); ?> "> <h4> <?php
/** * 从购物车创建订单 */ public function create_from_cart($cart, $pay_type, $pick_time, $user_name, $user_mobile, $address_province, $address_city, $address_area, $address_street, $order_remark) { $user_id = ''; $nums = 0; foreach ($cart as $key => $row) { if ($key == 0) { $user_id = $row->user_id; } $nums += $row->goods_nums; } $order_id = self::gen_orderid(); $this->order_id = $order_id; $this->user_id = $user_id; $this->create_time = W_START_TIME; // 订单数量 $this->order_nums = $nums; $this->user_name = $user_name; $this->user_mobile = $user_mobile; $this->address_province = $address_province; $this->address_city = $address_city; $this->address_area = $address_area; $this->address_street = $address_street; $this->cart_text = serialize($cart); $this->order_amount = $this->order_amount(); $this->total_amount = $this->total_amount(); $this->order_status = 0; $this->pick_time = $pick_time; $this->pay_type = $pay_type; if ($this->pay_type == 1) { $this->order_status = 1; } $this->pay_status = 0; $this->order_remark = $order_remark; $this->insert(); // 清理购物车 \Db\Trade\Cart::remove($user_id, 1); return $order_id; }
/** * 提交订单 */ public function confirm() { //\Core\View::$title = '确认订单'; $this->user = \Model\Authorize\Oauth::login_user(); $v = new \Model\Validation(); $v->required($this->user)->message('用户未登录', 1000); if (!$v->has_error()) { $address_id = \Core\URI::kv('address_id'); $pay_type = \Core\URI::kv('pay_type', 0); $pick_time = \Core\URI::kv('pick_time'); $order_remark = \Core\URI::kv('order_remark'); if (empty($pick_time)) { $pick_time = W_START_TIME; } else { $pick_time = strtotime($pick_time); } $goods_id = \Core\URI::kv('id'); $nums = \Core\URI::kv('nums'); $nums = abs(intval($nums)); if (!empty($goods_id)) { $cart = array(); $goods = \DB\Mall\Goods::row(array('goods_id' => $goods_id)); $v->required($goods)->message('商品不存在', 1000); $v->min_val($nums, 1)->message('数量不能小于1', 1000); if (!$v->has_error()) { $row = new \Db\Trade\Cart(); $row->user_id = $this->user->user_id; $row->goods_id = $goods->goods_id; $row->goods_nums = $nums; $row->goods_discount_price = $goods->goods_discount_price; $row->goods_original_price = $goods->goods_original_price; $row->cart_status = 1; $row->create_time = W_START_TIME; $row->goods = $goods; $cart[] = $row; //dump($cart);exit(); } } else { $cart = \Db\Trade\Cart::fetch(array('user_id' => $this->user->user_id, 'cart_status' => 1)); foreach ($cart as $c) { $c->goods->load(); } $v->filter_var(!empty($cart))->message('购物车为空', 1000); } $address = \DB\Account\Address::row(array('user_id' => $this->user->user_id, 'address_id' => $address_id)); $v->filter_var(!empty($address))->message('配送地址不存在', 1000); if (!$v->has_error()) { $order = new \Db\Trade\Order(); $order_id = $order->create_from_cart($cart, $pay_type, $pick_time, $address->user_name, $address->user_mobile, $address->address_province, $address->address_city, $address->address_area, $address->address_street, $order_remark); $v->set_data($order_id); } } $v->send(); }
public function get() { $this->view->content = view('main/get.php'); $id = \Core\URI::kv('id'); $this->view->content->row = new \DB\Mall\Goods($id); $this->view->content->user_id = $this->user->user_id; $this->view->content->count = \Db\Trade\Cart::count_cart($this->user->user_id); }