Reputation: 85
I use the session to create a shopping cart. I have a product model and a model for Variant, which I color and size my products in the Variant model. So that I can have products with different colors and sizes as well as different prices. The shopping cart I created with Session has a problem. The problem I have is that if the product has 3 different colors and each color has a different price, I have trouble displaying the price in the cart and I do not know how to act. How should I display the prices of my Variant model in the template?
my Product model:
class Product(models.Model):
name = models.CharField(max_length=200)
price = models.IntegerField(default=0)
my Variant model:
class Variants(models.Model):
name = models.CharField(max_length=100)
product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='product_var',)
size = models.ForeignKey(Size, on_delete=models.CASCADE)
color = models.ForeignKey(Color, on_delete=models.CASCADE)
price = models.IntegerField(default=0)
my session cart :
CART_SESSION_ID = 'cart'
class Cart:
def __init__(self, request):
self.session = request.session
cart = self.session.get(CART_SESSION_ID)
if not cart:
cart = self.session[CART_SESSION_ID] = {}
self.cart = cart
def __iter__(self):
product_ids = self.cart.keys()
products = Product.objects.filter(id__in=product_ids)
cart = self.cart.copy()
for product in products:
cart[str(product.id)]['product'] = product
def add(self, product, quantity):
product_id = str(product.id)
if product_id not in self.cart:
self.cart[product_id] = {'quantity': 0, 'price': str(product.total_price)}
self.cart[product_id]['quantity'] += quantity
self.save()
def save(self):
self.session.modified = True
my view:
# show cart
def cart_details(request):
cart = Cart(request)
return render(request, 'cart/cart_details.html', {'cart': cart, })
# add to cart
def cart_add(request, product_id):
cart = Cart(request)
product = get_object_or_404(Product, id=product_id)
form = CartAddForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
cart.add(product=product, quantity=cd['quantity'])
return redirect('cart:details')
my template for show product in cart:
{% for c in cart %}
<tr>
<td>{{ c.product.name }}</td>
<td>{{ c.product.price }}</td> # my problem!!!
</tr>
{% endfor %}
Upvotes: 2
Views: 4633
Reputation: 886
From the problem statement I see that problem will go away if you store variants in shopping cart instead of products.
The code will change to something like this:
class Cart:
def __init__(self, request):
self.session = request.session
cart = self.session.get(CART_SESSION_ID)
if not cart:
cart = self.session[CART_SESSION_ID] = {}
self.cart = cart
def __iter__(self):
variant_ids = self.cart.keys()
products = Variants.objects.filter(id__in=variant_ids)
cart = self.cart.copy()
for product in products:
cart[str(variant.id)]['variant'] = variant
def add(self, variant, quantity):
variant_id = str(variant.id)
if variant_id not in self.cart:
self.cart[variant_id] = {'quantity': 0, 'price': str(variant.price)}
self.cart[variant_id]['quantity'] += quantity
self.save()
def save(self):
self.session.modified = True
# add to cart
def cart_add(request, variant_id):
cart = Cart(request)
variant = get_object_or_404(Variant, id=variant_id)
form = CartAddForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
cart.add(variant=variant, quantity=cd['quantity'])
return redirect('cart:details')
template
{% for c in cart %}
<tr>
<td>{{ c.variant.name }}</td>
<td>{{ c.variant.price }}</td>
<td> color, etc.</td>
</tr>
{% endfor %}
You don't need to store the product in cart anymore since variant has relation with product table.
Upvotes: 1