def submit_order(request): if request.method == 'GET': username = request.session.get('username') user = User.objects.filter(username=username).first() carts = ShoppingCart.objects.filter(user=user, is_select=1).all() final_price = 0 if carts: for cart in carts: final_price += cart.nums * cart.goods.shop_price order = OrderInfo() order.user = user order.order_mount = final_price order.save() return HttpResponseRedirect(reverse('goods:index'))
def place_order(request): if request.method == 'GET': # 获取商品信息 carts = [] # 商品总数量 goods_count = 0 # 获取登录用户 token = request.COOKIES.get('token') user = UserToken.objects.filter(token=token).first().user # 用户地址信息 user_addresses = UserAddress.objects.filter(user=user).all() try: # 获取token,如果是商品详情点了立即购买 cart_id = request.GET.get('goods_id') cart_num = int(request.GET.get('goods_num')) # 从立即购买跳转来的 if cart_id: goods = Goods.objects.get(id=cart_id) goods_ = dict(model_to_dict(goods)) goods_['num'] = cart_num goods_['sum'] = cart_num * goods_['shop_price'] carts.append(goods_) goods_count = cart_num # 加入购物车 shop_carts = Cart() shop_carts.nums = cart_num shop_carts.goods = Goods.objects.filter(id=cart_id).first() shop_carts.user = User.objects.filter(username=user.username).first() shop_carts.is_select = 1 shop_carts.save() # 如果空的话 按购物车选中商品创建订单 except: cart_is = Cart.objects.filter(is_select=1).all() for cart in cart_is: goods = Goods.objects.get(id=cart.goods_id) goods_ = dict(model_to_dict(goods)) # 转字典添加数量key goods_['num'] = cart.nums goods_['sum'] = cart.nums * goods_['shop_price'] carts.append(goods_) goods_count = sum([i.nums for i in cart_is]) # 商品总价格 goods_sum = sum([i['num'] * i['shop_price'] for i in carts]) return render(request, 'web/place_order.html', {'user_addresses': user_addresses, 'carts': carts, 'goods_count': goods_count, 'goods_sum': goods_sum}) if request.method == 'POST': site_id = int(request.POST.get('site')) pay = int(request.POST.get('pay')) # 获取登录用户 token = request.COOKIES.get('token') user = UserToken.objects.filter(token=token).first().user # 商品信息 carts = [] cart_is = Cart.objects.filter(is_select=1).all() for cart in cart_is: goods = Goods.objects.get(id=cart.goods_id) goods_ = dict(model_to_dict(goods)) # 转字典添加数量key goods_['num'] = cart.nums carts.append(goods_) # 总价格和 +10 邮费 全不包邮 goods_sum = sum([i['num'] * i['shop_price'] for i in carts]) + 10 # 地址表 address = UserAddress.objects.filter(id=site_id).first() # 获取订单号 交易号 order_sn = get_order_sn() order = OrderInfo() # 保存信息 order.user = user order.order_sn = order_sn order.trade_no = get_trade_no() order.order_mount = goods_sum order.address = address.address order.signer_name = address.signer_name order.signer_mobile = address.signer_mobile order.save() # 保存关联的详情表 for i in cart_is: or_goods = OrderGoods() or_goods.order = OrderInfo.objects.filter(user=user).first() or_goods.order_sn = order_sn or_goods.goods = Goods.objects.filter(id=i.goods_id).first() or_goods.goods_nums = i.nums or_goods.save() # 修改商品库存 i.goods.goods_nums -= i.nums i.save() # 删除已下单的商品 cart_is.delete() return JsonResponse({'code': 200, 'msg': '请求成功'})