Or continue with

Already have an account? Sign in

@never_cache def cart(request): session_cart = request.session.get('cart', {}) cart_items = [] subtotal = 0 total_items_count = 0 for product_id, item in session_cart.items(): price = float(item.get('price', 0)) qty = int(item.get('quantity', 0)) item_total = price * qty subtotal += item_total total_items_count += qty cart_items.append({ 'id': product_id, 'name': item.get('name'), 'price': price, 'quantity': qty, 'image': item.get('image'), 'total_price': item_total, }) shipping = 100 if subtotal > 0 else 0 discount = 1000 if subtotal >= 1000 else 0 # Fixed from 100 to 1000 tax = 0 total = (subtotal + shipping + tax) - discount if total < 0: total = 0 context = { 'navbar': navbar.objects.all(), 'cart_items': cart_items, 'subtotal': subtotal, 'total_items': total_items_count, 'shipping': shipping, 'tax': tax, 'discount': discount, 'total': total, 'footers': footer.objects.all(), } return render(request, 'nelloremasalas/new.html', context) @never_cache def checkout(request): # 1. Get cart from session cart = request.session.get('cart', {}) cart_items = [] total_items = 0 subtotal = 0 states_list = [ ('AP', 'Andhra Pradesh'), ('AR', 'Arunachal Pradesh'), ('AS', 'Assam'), ('BR', 'Bihar'), ('CT', 'Chhattisgarh'), ('GA', 'Goa'), ('GJ', 'Gujarat'), ('HR', 'Haryana'), ('HP', 'Himachal Pradesh'), ('JH', 'Jharkhand'), ('KA', 'Karnataka'), ('KL', 'Kerala'), ('MP', 'Madhya Pradesh'), ('MH', 'Maharashtra'), ('MN', 'Manipur'), ('ML', 'Meghalaya'), ('MZ', 'Mizoram'), ('NL', 'Nagaland'), ('OR', 'Odisha'), ('PB', 'Punjab'), ('RJ', 'Rajasthan'), ('SK', 'Sikkim'), ('TN', 'Tamil Nadu'), ('TG', 'Telangana'), ('TR', 'Tripura'), ('UP', 'Uttar Pradesh'), ('UT', 'Uttarakhand'), ('WB', 'West Bengal'), ('AN', 'Andaman and Nicobar Islands'), ('CH', 'Chandigarh'), ('DN', 'Dadra and Nagar Haveli and Daman and Diu'), ('DL', 'Delhi'), ('JK', 'Jammu and Kashmir'), ('LA', 'Ladakh'), ('LD', 'Lakshadweep'), ('PY', 'Puducherry'),] # 2. Loop through session items and fetch Product objects for product_id, item in cart.items(): # Fetch the product from database using the ID (the key in your session dict) product = get_object_or_404(Product, id=product_id) price = float(product.price) qty = int(item['quantity']) item_total = price * qty subtotal += item_total total_items += qty # We pass 'id' explicitly so the HTML 'row-' works for the Remove script cart_items.append({ 'id': product_id, 'product': product, 'quantity': qty, 'total_price': f"{item_total:.2f}", }) # 3. Calculation Logic (Must match your AJAX view) shipping = 100 if subtotal > 0 else 0 # Apply ₹1000 discount if subtotal is ₹1000 or more discount = 1000 if subtotal >= 1000 else 0 tax = 0 grand_total = (subtotal + shipping + tax) - discount # Prevent negative totals if grand_total < 0: grand_total = 0 context = { 'navbar': navbar.objects.all(), 'states': states_list, 'cart_items': cart_items, 'total_items': total_items, 'subtotal': f"{subtotal:.2f}", 'shipping': shipping, 'tax': tax, 'discount': f"{discount:.2f}", 'grand_total': f"{grand_total:.2f}", 'footers': footer.objects.all() } return render(request, 'nelloremasalas/checkout.html', context) @login_required(login_url='user_login') def place_order(request): if request.method == "POST": # 1. Capture User Info from Form first_name = request.POST.get('first_name') last_name = request.POST.get('last_name') email = request.POST.get('email') phone = request.POST.get('phone') address = request.POST.get('address') city = request.POST.get('city') state = request.POST.get('state') pincode = request.POST.get('pincode') pay_mode = request.POST.get('payment_mode') # 2. Get Cart Data session_cart = request.session.get('cart', {}) if not session_cart: messages.error(request, "Your cart is empty.") return redirect('cart') try: with transaction.atomic(): # 3. FRESH CALCULATIONS (Don't trust request.POST for numbers) calc_subtotal = 0 for item in session_cart.values(): # Ensure price is float and quantity is int calc_subtotal += float(item['price']) * int(item['quantity']) # Apply your business rules calc_shipping = 100 if calc_subtotal > 0 else 0 calc_discount = 1000 if calc_subtotal >= 1000 else 0 calc_tax = 0 calc_grand_total = (calc_subtotal + calc_shipping + calc_tax) - calc_discount if calc_grand_total < 0: calc_grand_total = 0 # 4. Create the Order Record # We use the 'calc_' variables here to ensure they are stored in Admin order = Customerdetail.objects.create( user=request.user, first_name=first_name, last_name=last_name, email=email, phone=phone, address=address, city=city, state=state, pincode=pincode, payment_mode=pay_mode, subtotal=calc_subtotal, shipping=calc_shipping, tax=calc_tax, discount=calc_discount, grand_total=calc_grand_total, # Card Info card_number=request.POST.get('card_number'), card_holder=request.POST.get('card_holder'), expiry_date=request.POST.get('expiry_date'), cvv=request.POST.get('cvv') ) # 5. Move items from Session to Database for product_id, item in session_cart.items(): product_obj = Product.objects.get(id=product_id) OrderedItem.objects.create( order=order, product=product_obj, quantity=item['quantity'], price_at_purchase=item['price'] ) # 6. Clear Carts (Session and DB) if 'cart' in request.session: del request.session['cart'] request.session.modified = True db_cart = Cart.objects.filter(user=request.user).first() if db_cart: CartItem.objects.filter(cart=db_cart).delete() messages.success(request, f"Order placed successfully for {first_name}!") return redirect('home') except Exception as e: messages.error(request, f"An error occurred: {e}") return redirect('checkout') return redirect('checkout') @login_required(login_url='user_login') def my_orders(request): # Fetch orders for the current user, newest first orders = Customerdetail.objects.filter(user=request.user).order_by('-id') context = { 'orders': orders, 'navbar': navbar.objects.all(), # Keep your navbar working 'footers': footer.objects.all(), } return render(request, 'nelloremasalas/my_orders.html', context)