允许使用相同product_id但不同product_options的add_cart的Django电子商务视图.<



我正在写一个django电子商务应用程序,它有多个产品选项,可以选择性地添加到相同的productid。我在获取视图以识别具有不同产品选项的相同产品作为单独的购物车项目时遇到了问题。我让它工作大约一半的时间,但只有当一个产品通过没有选项,然后添加选项的后续项目不是一个问题。如果您尝试将没有选项的裸产品添加到购物车中,并且该产品具有现有的相同产品id和选项,它要么抛出错误代码,要么将选项添加到现有的购物车项目并将其加1

views.py

def add_cart(request, product_id, quantity=1):
product = Product.objects.get(id = product_id) # get the product  
product_options= request.POST.getlist('option')#gets selected product options   

#
try:
cart = Cart.objects.get(cart_id=_cart_id(request)) # get the cart using the cart_id present in the session
except Cart.DoesNotExist:
cart = Cart.objects.create(
cart_id = _cart_id(request)
)
cart.save()#saves new cart if needed to be created   
try:
cart = Cart.objects.get(cart_id=_cart_id(request)) # get the cart using the cart_id present in the session
except Cart.DoesNotExist:
cart = Cart.objects.create(
cart_id = _cart_id(request)
)
cart.save()#saves new cart if needed to be created 

try: 
cart_item = CartItem.objects.get(product = product, cart = cart)
# if the product has multiple options, check if any of the options in the cart item matches the selected options 
if len(product_options) > 0:
match = False
for option in cart_item.options.all():
if option in product_options:
match = True
cart_item.quantity += quantity
cart_item.save()
# if none of the cart item options matches the selected options, add new cart item with selected options 
if not match:
cart_item = CartItem.objects.create(
product = product,
cart = cart,
quantity = quantity,
)
if len(product_options) > 0:
for item in product_options:
cart_item.options.add(item)
cart_item.save() 
else:
cart_item.quantity += quantity # adds the specified quantity of a single instance of product cart item
cart_item.save()  
except CartItem.DoesNotExist:
cart_item = CartItem.objects.create(
product = product,
cart = cart,
quantity = quantity
)
if len(product_options) > 0:
for item in product_options:
cart_item.options.add(item)
cart_item.save() 
return redirect("cart")  

我认为主要的问题是,当视图决定创建一个新实例时,视图最初是在寻找匹配的选项,而不管是否有后续选项。我需要所有的选项都存在,并有视图增量+=1或创建一个新的cart_item实例。

我不知道如何有效地循环通过它,任何帮助将不胜感激

有趣,我明白你想要改进代码的原因:

  1. 购物车大小写:"获取已存在的,否则创建一个新的。">
  2. CartItem case: "如果有,请添加更多数量,否则,创建一个新的数量。">

我认为在这些情况下我们可以使用get_or_create()

这些代码只是我的一些想法,你可以告诉我你的意见,然后我会试着帮助你=)

from django.db.models import F

def add_cart(request, product_id, quantity=1):
product = Product.objects.get(id = product_id) # get the product  
product_options= request.POST.getlist('option') # gets selected product options 
# get the cart using the cart_id present in the session
cart, created = Cart.objects.get_or_create(cart_id=_cart_id(request))
# if the product has multiple options, check if any of the options in the cart item matches the selected options
cart_item, created = CartItem.objects.get_or_create(
product=product, cart=cart,
defaults={
'product': product,
'cart': cart,
'quantity': quantity
}
)
for option in cart_item.options.all():
if option in product_options:
cart_item.quantity += quantity
else:
cart_item.options.add(item)
cart_item.save()
return redirect("cart")  

相关内容

  • 没有找到相关文章