Django电商购物车:自定义订单存在时无法添加普通商品问题排查
Django电商购物车问题:含自定义订单时无法添加普通商品
我开发的Django电商网站已实现自定义订单加入购物车的功能,但当购物车中存在自定义订单时,无法添加普通商品。尝试临时从session的bag中移除custom_order,待添加商品的代码执行后再重新加入,但该方案无效且无报错,推测是逻辑存在问题。
现有代码
context.py
from decimal import Decimal from django.conf import settings from django.shortcuts import get_object_or_404 from products.models import Product from custom_order.models import CustomOrder def bag_contents(request): bag_items = [] total = 0 bag = request.session.get('bag', {}) print(bag) if 'custom_order' in bag: custom_order_id = bag['custom_order'] custom_order = CustomOrder.objects.get(pk=custom_order_id) total += custom_order.price bag_items.append({ 'product_id': custom_order.product_id, 'custom_order_id': custom_order_id, 'category': custom_order.category, 'material': custom_order.material, 'individual_total': custom_order.price, 'quantity': 1, 'product_name': custom_order.product_name }) else: for product_id, quantity in bag.items(): product = Product.objects.get(pk=product_id) if isinstance(quantity, int) and isinstance(product.price, Decimal): total += quantity * product.price individual_total = quantity * product.price product_count += quantity bag_items.append({ 'product_id': product_id, 'quantity': quantity, 'product': product, 'individual_total': individual_total, }) grand_total = total context = { 'bag_items': bag_items, 'grand_total': grand_total, } return context
添加普通商品到购物车视图
def add_to_bag(request, product_id): """ Add a quantity of the specified product to the shopping bag """ quantity = int(request.POST.get('quantity')) redirect_url = request.POST.get('redirect_url') bag = request.session.get('bag', {}) if product_id in list(bag.keys()): bag[product_id] += quantity else: bag[product_id] = quantity request.session['bag'] = bag return redirect(redirect_url)
添加自定义订单到购物车视图
def add_custom_order_to_bag(request, custom_order_id): """ Add a quantity of the specified product to the shopping bag """ custom_order = CustomOrder.objects.get(pk=custom_order_id) bag = request.session.get('bag', {}) bag['custom_order'] = custom_order_id request.session['bag'] = bag return redirect(reverse('view_bag'))
尝试的修改方案
def add_to_bag(request, product_id): """ Add a quantity of the specified product to the shopping bag """ quantity = int(request.POST.get('quantity')) redirect_url = request.POST.get('redirect_url') bag = request.session.get('bag', {}) custom_order_id = bag.pop('custom_order', None) if product_id in list(bag.keys()): bag[product_id] += quantity else: bag[product_id] = quantity if custom_order_id: bag['custom_order'] = custom_order_id request.session['bag'] = bag return redirect(redirect_url)
问题分析
核心问题出在bag_contents函数的逻辑上:当bag中存在custom_order时,代码只会处理自定义订单,进入if分支后完全跳过了普通商品的遍历逻辑(else分支)。实际上你修改的add_to_bag是有效的,普通商品已经被加入到session的bag中,但因为bag_contents没有处理这些商品,前端无法显示,导致看起来像是添加失败。
解决办法
修改bag_contents函数,去掉else分支,改为先处理自定义订单(如果有),再遍历处理所有普通商品(跳过custom_order这个key),这样无论购物车中是否有自定义订单,普通商品都会被正常处理并加入到bag_items中。
修改后的bag_contents代码:
from decimal import Decimal from django.conf import settings from django.shortcuts import get_object_or_404 from products.models import Product from custom_order.models import CustomOrder def bag_contents(request): bag_items = [] total = 0 product_count = 0 # 修复未定义的变量 bag = request.session.get('bag', {}) print(bag) # 先处理自定义订单 if 'custom_order' in bag: custom_order_id = bag['custom_order'] custom_order = CustomOrder.objects.get(pk=custom_order_id) total += custom_order.price bag_items.append({ 'product_id': custom_order.product_id, 'custom_order_id': custom_order_id, 'category': custom_order.category, 'material': custom_order.material, 'individual_total': custom_order.price, 'quantity': 1, 'product_name': custom_order.product_name }) # 再处理所有普通商品,跳过custom_order键 for item_key, quantity in bag.items(): if item_key == 'custom_order': continue product = Product.objects.get(pk=item_key) if isinstance(quantity, int) and isinstance(product.price, Decimal): total += quantity * product.price individual_total = quantity * product.price product_count += quantity bag_items.append({ 'product_id': item_key, 'quantity': quantity, 'product': product, 'individual_total': individual_total, }) grand_total = total context = { 'bag_items': bag_items, 'grand_total': grand_total, 'product_count': product_count, # 如果需要用到这个变量,加入context } return context
另外注意原代码中product_count变量未定义,已在修改后的代码中添加定义并加入context(如果前端需要显示商品总数的话)。
内容的提问来源于stack exchange,提问作者Leonie
相关产品推荐
相关产品推荐

