diff --git a/artel/store/cart.py b/artel/store/cart.py index bb80ceb..814f879 100644 --- a/artel/store/cart.py +++ b/artel/store/cart.py @@ -66,14 +66,17 @@ class SessionCart(BaseCart): # TODO - add logging ... - def update_item_quantity(self, item_id: int, change: int) -> None: + def update_item_quantity(self, item_id: int, new_quantity: int) -> None: self.validate_item_id(item_id) + if new_quantity < 1: + self.remove_item(item_id) + return try: - self.session[settings.CART_SESSION_ID][str(item_id)] += change + self.session[settings.CART_SESSION_ID][str(item_id)] = new_quantity self.session.modified = True except KeyError: # TODO - add logging - self.add_item(item_id, change) + self.add_item(item_id, new_quantity) def get_items(self) -> List[CartItem]: _items = [] diff --git a/artel/store/static/js/cart.js b/artel/store/static/js/cart.js index 32d5d33..34cf520 100644 --- a/artel/store/static/js/cart.js +++ b/artel/store/static/js/cart.js @@ -122,16 +122,18 @@ $(document).on('click', '.add-to-cart-button', function(event) { const productID = $(this).data('product-id'); const newQuantity = input.val(); const csrfToken = $(this).data('csrf-token'); + const url = $(this).data("update-cart-url") formData.append('product_id', productID); formData.append('quantity', newQuantity); - + console.log(input.val()) $.ajax({ - type: 'POST', - url: '/../../cart/item/', + type: 'PUT', + url: url, data: formData, // Use the serialized form data headers: { 'X-CSRFToken': csrfToken }, dataType: 'json', + success: location.reload(), processData: false, // Prevent jQuery from processing the data contentType: false, // Let the browser set the content type }); diff --git a/artel/store/templates/store/cart.html b/artel/store/templates/store/cart.html index 4dca58e..5ecf9b3 100644 --- a/artel/store/templates/store/cart.html +++ b/artel/store/templates/store/cart.html @@ -6,7 +6,7 @@
-

Shopping Cart

+

Koszyk

{% for item in cart.get_items %} @@ -17,10 +17,10 @@
-
To Pay: {{cart.total_price}}
+
Do zapłaty: {{cart.total_price}}
diff --git a/artel/store/templates/store/order.html b/artel/store/templates/store/order.html index ee14e34..a8fdc65 100644 --- a/artel/store/templates/store/order.html +++ b/artel/store/templates/store/order.html @@ -8,8 +8,7 @@
-

Order Data

- We won't share your data +

Twoje dane


@@ -84,7 +83,7 @@
- +
diff --git a/artel/store/templates/store/order_confirm.html b/artel/store/templates/store/order_confirm.html index 73d0d22..4c1ee87 100644 --- a/artel/store/templates/store/order_confirm.html +++ b/artel/store/templates/store/order_confirm.html @@ -5,13 +5,13 @@
-

Customer Data

+

Twoje dane:

-

Full Name

+

Imię i Nazwisko

{{customer_data.full_name}}

@@ -29,7 +29,7 @@
-

Phone

+

Numer telefonu

{{customer_data.phone}}

@@ -38,7 +38,7 @@
-

Address

+

Adres

{{customer_data.full_address}}

@@ -51,7 +51,7 @@
-

Order Items

+

Zamówione przedmioty

{% for item in cart.get_items %} @@ -62,12 +62,12 @@
-
To Pay: {{cart.total_price}}
+
Do zapłaty: {{cart.total_price}}
{% csrf_token %} - +
diff --git a/artel/store/templates/store/partials/cart_item.html b/artel/store/templates/store/partials/cart_item.html index 0e563f7..52c2c41 100644 --- a/artel/store/templates/store/partials/cart_item.html +++ b/artel/store/templates/store/partials/cart_item.html @@ -18,7 +18,10 @@ + class="form-control form-control-sm quantity-input" + data-product-id="{{item.product.id}}" + data-csrf-token="{{csrf_token}}" + data-update-cart-url="{% url 'cart-action-update-product' item.product.id %}"/>
@@ -36,13 +36,13 @@ diff --git a/artel/store/views.py b/artel/store/views.py index 9b47681..87f5922 100644 --- a/artel/store/views.py +++ b/artel/store/views.py @@ -74,10 +74,9 @@ class CartActionView(ViewSet): return Response(serializer.data, status=201) @action(detail=True, methods=["put"]) - def update_product(self, request, product_id): + def update_product(self, request, pk): cart = SessionCart(self.request) - product_id = request.POST.get("product_id") - cart.update_item_quantity(product_id, request.PUT["quantity"]) + cart.update_item_quantity(pk, int(request.data["quantity"])) items = cart.get_items() serializer = CartProductSerializer(instance=items, many=True) return Response(serializer.data, status=201)