From 30ca0ef20c31c6b341ae911e50369999c5cad10d Mon Sep 17 00:00:00 2001 From: Raz Date: Mon, 26 May 2025 12:24:22 +0200 Subject: [PATCH] fix shop shipping --- shop/templates/shop/cart.html | 21 ++++++++++++++------- shop/views.py | 8 +++----- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/shop/templates/shop/cart.html b/shop/templates/shop/cart.html index 43f5401..1efb018 100644 --- a/shop/templates/shop/cart.html +++ b/shop/templates/shop/cart.html @@ -131,16 +131,23 @@ const subtotalAmount = document.getElementById('subtotal-amount'); const discountAmount = document.getElementById('discount-amount'); const finalTotal = document.getElementById('final-total'); - const shippingForm = document.getElementById('shipping-form'); + + // Get address input elements directly + const streetAddress = document.getElementById('street-address'); + const apartment = document.getElementById('apartment'); + const postalCode = document.getElementById('postal-code'); + const city = document.getElementById('city'); + const country = document.getElementById('country'); // Function to collect shipping address data function getShippingData() { - const formData = new FormData(shippingForm); - const shippingData = {}; - formData.forEach((value, key) => { - shippingData[key] = value; - }); - return shippingData; + return { + street_address: streetAddress ? streetAddress.value : '', + apartment: apartment ? apartment.value : '', + postal_code: postalCode ? postalCode.value : '', + city: city ? city.value : '', + country: country ? country.value : '' + }; } // Initial values diff --git a/shop/views.py b/shop/views.py index 2a3f303..1e4c9e3 100644 --- a/shop/views.py +++ b/shop/views.py @@ -463,6 +463,7 @@ def create_checkout_session(request): if not request.user.is_authenticated: return JsonResponse({'error': 'User must be authenticated'}, status=403) + shipping_address = None # Parse shipping address data from request try: data = json.loads(request.body) @@ -472,11 +473,8 @@ def create_checkout_session(request): # Validate shipping address shipping_form = ShippingAddressForm(shipping_data) - if not shipping_form.is_valid(): - return JsonResponse({'error': 'Invalid shipping address'}, status=400) - - # Save shipping address - shipping_address = shipping_form.save() + if shipping_form.is_valid(): + shipping_address = shipping_form.save() # Create the order with shipping address order = create_order(request)