public function two() { if (empty($this->user_session)) { echo json_encode(array('status' => false, 'msg' => 'nologin')); exit; } $id = $_POST['id']; if (empty($id)) { echo json_encode(array('status' => false, 'msg' => '提交信息不正确')); exit; } $user_cart_list = D('User_cart')->where(array('pigcms_id' => array('in', $id), 'uid' => $this->user_session['uid']))->select(); if (empty($user_cart_list)) { echo json_encode(array('status' => false, 'msg' => '提交的购物车为空')); exit; } $store_id = 0; $total = 0; $total_money = 0; $product_list = array(); foreach ($user_cart_list as $key => &$user_cart) { if ($key == 0) { $store_id = $user_cart['store_id']; } else { if ($store_id != $user_cart['store_id']) { echo json_encode(array('status' => false, 'msg' => '暂时只支持单店铺提交订单')); exit; } } $product = D('Product')->where(array('product_id' => $user_cart['product_id'], 'status' => 1))->find(); if (empty($product)) { echo json_encode(array('status' => false, 'msg' => '未找到相应的产品')); exit; } // 限购 if ($product['buyer_quota']) { $number = M('Order_product')->getProductPronum($product['product_id'], $this->user_session['uid']); if ($number + $user_cart['pro_num'] > $product['buyer_quota']) { echo json_encode(array('status' => false, 'msg' => '对不起,您超出了限购')); exit; } } $sku = array(); // 查找库存 if ($product['has_property'] == 0) { if ($product['quantity'] < $user_cart['pro_num']) { echo json_encode(array('status' => false, 'msg' => $product['name'] . '的库存不足')); exit; } } else { $sku = D('Product_sku')->where(array('sku_id' => $user_cart['sku_id']))->find(); if ($sku['quantity'] < $user_cart['pro_num']) { echo json_encode(array('status' => false, 'msg' => $product['name'] . '的库存不足')); exit; } } $product_list[$product['product_id']] = $product['product_id']; $total += $user_cart['pro_num']; $total_money += $user_cart['pro_price'] * $user_cart['pro_num']; $user_cart['product'] = $product; $user_cart['sku'] = $sku; $user_cart['sku_data'] = $user_cart['sku_data']; } $store = M('Store')->getStore($store_id); if (empty($store['buyer_selffetch']) && empty($store['open_logistics'])) { echo json_encode(array('status' => false, 'msg' => '店铺未设置物流配送方式,暂时不能购买')); exit; } //----------------------------------------------------- // 在提交购物车的内容手就产生订单,无须到第二步确认订单 // 生成订单 $order_no = date('YmdHis', $_SERVER['REQUEST_TIME']) . mt_rand(100000, 999999); $data_order['store_id'] = $store_id; $data_order['order_no'] = $data_order['trade_no'] = $order_no; $data_order['uid'] = $this->user_session['uid']; $data_order['sub_total'] = $total_money; $data_order['pro_num'] = $total; $data_order['pro_count'] = count($product_list); $data_order['add_time'] = $_SERVER['REQUEST_TIME']; $database = D('Order'); $order_id = $database->data($data_order)->add(); if (empty($order_id)) { echo json_encode(array('status' => false, 'msg' => '订单产生失败')); exit; } M('Store_user_data')->upUserData($data_order['store_id'], $this->user_session['uid'], 'unpay'); $database_order_product = D('Order_product'); $database_product = D('Product'); $data_order_product['order_id'] = $order_id; $suppliers = array(); foreach ($user_cart_list as $value) { $product_info = $database_product->field('store_id, original_product_id, weight')->where(array('product_id' => $value['product_id']))->find(); if (!empty($product_info['original_product_id'])) { $tmp_product_info = $database_product->field('store_id')->where(array('product_id' => $product_info['original_product_id']))->find(); $supplier_id = $tmp_product_info['store_id']; $original_product_id = $product_info['original_product_id']; } else { $supplier_id = $product_info['store_id']; $original_product_id = $value['product_id']; } $data_order_product['supplier_id'] = $supplier_id; $data_order_product['original_product_id'] = $original_product_id; $data_order_product['user_order_id'] = $order_id; $suppliers[] = $supplier_id; $data_order_product['product_id'] = $value['product_id']; $data_order_product['sku_id'] = $value['sku_id']; $data_order_product['sku_data'] = $value['sku_data']; $data_order_product['pro_num'] = $value['pro_num']; $data_order_product['pro_price'] = $value['pro_price']; $data_order_product['comment'] = !empty($value['comment']) ? $value['comment'] : ''; $data_order_product['is_fx'] = $value['is_fx']; //是否是分销商品 $data_order_product['pro_weight'] = $product_info['weight']; $database_order_product->data($data_order_product)->add(); } $suppliers = array_unique($suppliers); //分销商 $suppliers = implode(',', $suppliers); if (!empty($suppliers)) { //修改订单,设置分销商 $database->where(array('order_id' => $order_id))->data(array('suppliers' => $suppliers))->save(); } $condition_user_cart = array('uid' => $this->user_session['uid'], 'pigcms_id' => array('in', $id)); D('User_cart')->where($condition_user_cart)->delete(); // 产生提醒 import('source.class.Notify'); Notify::createNoitfy($product['store_id'], option('config.orderid_prefix') . $order_no); //redirect(url('order:address', array('order_id' => option('config.orderid_prefix') . $order_no))); echo json_encode(array('status' => true, 'data' => array('order_no' => option('config.orderid_prefix') . $order_no))); exit; //---------------------------------------------------- $user_address_list = M('User_address')->select('', $this->user_session['uid']); $this->assign('user_address_list', $user_address_list); $this->assign('user_cart_list', $user_cart_list); $this->assign('total', $total); $this->assign('total_money', $total_money); $this->assign('step', 'two'); $this->display(); }
function add() { if (empty($this->user_session)) { echo json_encode(array('status' => false, 'msg' => 'nologin')); exit; } // 参数获取 $product_id = $_POST['product_id']; $sku_id = $_POST['sku_id']; $quantity = $_POST['quantity']; //验证商品 $product = D('Product')->field('`product_id`,`store_id`,`price`,`has_property`,`status`,`supplier_id`,`quantity`, `buyer_quota`, `weight`')->where(array('product_id' => $product_id))->find(); if (empty($product) || empty($product['status'])) { echo json_encode(array('status' => false, 'msg' => '商品不存在')); exit; } // 限购 if ($product['buyer_quota']) { $number = M('Order_product')->getProductPronum($product_id, $this->user_session['uid']); // 如果加入购物车,得计算购物车里同产品数量 if ($_POST['type'] != 'add') { $user_cart_num = D('User_cart')->field('pro_num')->where(array('uid' => $this->user_session['uid'], 'product_id' => $product_id))->find(); $number += $user_cart_num['pro_num']; } if ($number + $quantity > $product['buyer_quota']) { echo json_encode(array('status' => false, 'msg' => '对不起,您超出了限购')); exit; } } if (empty($product['has_property'])) { $sku_id = 0; $propertiesStr = ''; $product_price = $product['price']; if ($product['quantity'] < $quantity) { echo json_encode(array('status' => false, 'msg' => '商品库存不足')); exit; } } else { if (empty($sku_id)) { echo json_encode(array('status' => false, 'msg' => '请选择商品属性')); exit; } //判断库存是否存在 $nowSku = D('Product_sku')->field('`sku_id`,`product_id`,`properties`,`price`, `quantity`')->where(array('sku_id' => $sku_id))->find(); if ($nowSku['quantity'] < $quantity) { echo json_encode(array('status' => false, 'msg' => '商品库存不足')); exit; } $tmpPropertiesArr = explode(';', $nowSku['properties']); $properties = $propertiesValue = $productProperties = array(); foreach ($tmpPropertiesArr as $value) { $tmpPro = explode(':', $value); $properties[] = $tmpPro[0]; $propertiesValue[] = $tmpPro[1]; } if (count($properties) == 1) { $findPropertiesArr = D('Product_property')->field('`pid`,`name`')->where(array('pid' => $properties[0]))->select(); $findPropertiesValueArr = D('Product_property_value')->field('`vid`,`value`')->where(array('vid' => $propertiesValue[0]))->select(); } else { $findPropertiesArr = D('Product_property')->field('`pid`,`name`')->where(array('pid' => array('in', $properties)))->select(); $findPropertiesValueArr = D('Product_property_value')->field('`vid`,`value`')->where(array('vid' => array('in', $propertiesValue)))->select(); } foreach ($findPropertiesArr as $value) { $propertiesArr[$value['pid']] = $value['name']; } foreach ($findPropertiesValueArr as $value) { $propertiesValueArr[$value['vid']] = $value['value']; } foreach ($properties as $key => $value) { $productProperties[] = array('pid' => $value, 'name' => $propertiesArr[$value], 'vid' => $propertiesValue[$key], 'value' => $propertiesValueArr[$propertiesValue[$key]]); } $propertiesStr = serialize($productProperties); if ($product['product_id'] != $nowSku['product_id']) { echo json_encode(array('status' => false, 'msg' => '商品属性选择错误')); exit; } $product_price = $nowSku['price']; } if ($_POST['activityId']) { $nowActivity = M('Product_qrcode_activity')->getActivityById($_POST['activityId']); if ($nowActivity['product_id'] == $product['product_id']) { if ($nowActivity['type'] == 0) { $product_price = round($product_price * $nowActivity['discount'] / 10, 2); } else { $product_price = $product_price - $nowActivity['price']; } } } if (empty($quantity)) { echo json_encode(array('status' => false, 'msg' => '请输入购买数量')); exit; } if ($_POST['type'] == 'add') { //立即购买 $order_no = date('YmdHis', $_SERVER['REQUEST_TIME']) . mt_rand(100000, 999999); $data_order['store_id'] = $product['store_id']; $data_order['order_no'] = $data_order['trade_no'] = $order_no; $data_order['uid'] = $this->user_session['uid']; $data_order['sub_total'] = $product_price * 100 * $quantity / 100; $data_order['pro_num'] = $quantity; $data_order['pro_count'] = '1'; $data_order['add_time'] = $_SERVER['REQUEST_TIME']; // 收货地址 /*$address = M('User_address')->getAdressById('', $this->user_session['uid'], $address_id); $address_arr = array(); if (!empty($address)) { $address_arr['address'] = $address['address']; $address_arr['province'] = $address['province_txt']; $address_arr['province_code'] = $address['province']; $address_arr['city'] = $address['city_txt']; $address_arr['city_code'] = $address['city']; $address_arr['area'] = $address['area_txt']; $address_arr['area_code'] = $address['area']; } $address_list = M('User_address')->getMyAddress($this->user_session['uid']); // 收货地址 $address = array(); if (!empty($address_list)) { $address = $address_list[0]; } $address_arr = array(); if (!empty($address)) { $address_arr['address'] = $address['address']; $address_arr['province'] = $address['province_txt']; $address_arr['province_code'] = $address['province']; $address_arr['city'] = $address['city_txt']; $address_arr['city_code'] = $address['city']; $address_arr['area'] = $address['area_txt']; $address_arr['area_code'] = $address['area']; } $data_order['address'] = serialize($address_arr); $data_order['address_user'] = $address['name']; $data_order['address_tel'] = $address['tel'];*/ $order_id = D('Order')->data($data_order)->add(); if (empty($order_id)) { echo json_encode(array('status' => false, 'msg' => '订单产生失败,请重试')); exit; } $data_order_product['order_id'] = $order_id; $data_order_product['product_id'] = $product['product_id']; $data_order_product['sku_id'] = $sku_id; $data_order_product['sku_data'] = $propertiesStr; $data_order_product['pro_num'] = $quantity; $data_order_product['pro_price'] = $product_price; $data_order_product['comment'] = !empty($_POST['custom']) ? serialize($_POST['custom']) : ''; $data_order_product['pro_weight'] = $product['weight']; //分销商品 if (!empty($product['supplier_id'])) { $data_order_product['is_fx'] = 1; } $product_info = D('Product')->field('product_id,store_id,original_product_id')->where(array('product_id' => $product['product_id']))->find(); if (!empty($product_info['original_product_id'])) { $tmp_product_info = D('Product')->field('store_id')->where(array('product_id' => $product_info['original_product_id']))->find(); $supplier_id = $tmp_product_info['store_id']; $original_product_id = $product_info['original_product_id']; } else { $supplier_id = $product_info['store_id']; $original_product_id = $product_info['product_id']; } $data_order_product['supplier_id'] = $supplier_id; $data_order_product['original_product_id'] = $original_product_id; $data_order_product['user_order_id'] = $order_id; if (D('Order_product')->data($data_order_product)->add()) { if (!empty($this->user_session['uid'])) { M('Store_user_data')->upUserData($product['store_id'], $this->user_session['uid'], 'unpay'); } if (!empty($supplier_id)) { //修改订单,设置分销商 D('Order')->where(array('order_id' => $order_id))->data(array('suppliers' => $supplier_id))->save(); } echo json_encode(array('status' => true, 'msg' => '订单添加成功', 'data' => array('order_no' => option('config.orderid_prefix') . $order_no))); // 产生提醒 import('source.class.Notify'); Notify::createNoitfy($product['store_id'], option('config.orderid_prefix') . $order_no); exit; } else { echo json_encode(array('status' => false, 'msg' => '订单产生失败,请重试')); exit; } } else { // 查找购物车里是否有相应的产品 $user_cart = D('User_cart')->where(array('uid' => $this->user_session['uid'], 'product_id' => $product['product_id'], 'store_id' => $product['store_id'], 'sku_id' => $sku_id))->find(); if (!empty($user_cart)) { if (D('User_cart')->where(array('pigcms_id' => $user_cart['pigcms_id']))->setInc('pro_num', $quantity)) { // 查找购物车数量 $pro_num = D('User_cart')->where(array('uid' => $this->user_session['uid']))->sum('pro_num'); echo json_encode(array('status' => true, 'msg' => '添加购物车成功', 'data' => array('number' => $pro_num))); exit; } else { echo json_encode(array('status' => false, 'msg' => '添加购物车失败')); exit; } } $data_user_cart['uid'] = $this->user_session['uid']; $data_user_cart['product_id'] = $product['product_id']; $data_user_cart['store_id'] = $product['store_id']; $data_user_cart['sku_id'] = $sku_id; $data_user_cart['sku_data'] = $propertiesStr; $data_user_cart['pro_num'] = $quantity; $data_user_cart['pro_price'] = $product_price; $data_user_cart['add_time'] = $_SERVER['REQUEST_TIME']; $data_user_cart['comment'] = !empty($_POST['custom']) ? serialize($_POST['custom']) : ''; if (!empty($product['supplier_id'])) { $data_user_cart['is_fx'] = 1; } if (D('User_cart')->data($data_user_cart)->add()) { // 查找购物车数量 $pro_num = D('User_cart')->where(array('uid' => $this->user_session['uid']))->sum('pro_num'); echo json_encode(array('status' => true, 'msg' => '添加购物车成功', 'data' => array('number' => $pro_num))); exit; } else { echo json_encode(array('status' => false, 'msg' => '添加购物车失败')); exit; } } }
$database_order_product->data($data_order_product)->add(); } $suppliers = array_unique($suppliers); //分销商 $suppliers = implode(',', $suppliers); if (!empty($suppliers)) { //修改订单,设置分销商 $data = array(); $data['suppliers'] = $suppliers; if (!empty($suppliers) && $suppliers != $_POST['storeId']) { $data['is_fx'] = 1; } $database->where(array('order_id' => $order_id))->data($data)->save(); } //删除购物车商品 $condition_user_cart['pigcms_id'] = array('in', $_POST['ids']); if (!empty($wap_user['uid'])) { $condition_user_cart['uid'] = $wap_user['uid']; } else { $condition_user_cart['session_id'] = session_id(); } D('User_cart')->where($condition_user_cart)->delete(); // 产生提醒 import('source.class.Notify'); Notify::createNoitfy($_POST['storeId'], option('config.orderid_prefix') . $order_no); json_return(0, $config['orderid_prefix'] . $order_no); } } } } echo ob_get_clean();
if (D('Order_product')->data($data_order_product)->add()) { if (!empty($wap_user['uid'])) { M('Store_user_data')->upUserData($nowProduct['store_id'], $wap_user['uid'], 'unpay'); } if (!empty($supplier_id)) { //修改订单,设置分销商 $data = array(); $data['suppliers'] = $supplier_id; if (!empty($supplier_id) && $supplier_id != $nowProduct['store_id']) { $data['is_fx'] = 1; } D('Order')->where(array('order_id' => $order_id))->data($data)->save(); } // 产生提醒 import('source.class.Notify'); Notify::createNoitfy($nowProduct['store_id'], option('config.orderid_prefix') . $order_no); json_return(0, $config['orderid_prefix'] . $order_no); } else { D('Order')->where(array('order_id' => $order_id))->delete(); json_return(1005, '订单产生失败,请重试'); } } else { if (!empty($wap_user['uid'])) { $data_user_cart['uid'] = $wap_user['uid']; } else { $data_user_cart['session_id'] = session_id(); } $data_user_cart['product_id'] = $nowProduct['product_id']; $data_user_cart['store_id'] = $nowProduct['store_id']; $data_user_cart['sku_id'] = $skuId; $data_user_cart['sku_data'] = $propertiesStr;