Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add function for changing quantity in cart

weronkabee51942106f
diff --git a/saleor/static/js/storefront.js b/saleor/static/js/storefront.js
index d4cb90cb06..a5c5f3a346 100644
--- a/saleor/static/js/storefront.js
+++ b/saleor/static/js/storefront.js
@@ -131,17 +131,56 @@ $countrySelect.on('change', (e) => {
})
})
-// Save tab links to URL
-
-$('.nav-tabs a').click((e) => {
- e.preventDefault();
- $(this).tab('show');
-});
-
-$("ul.nav-tabs li a:not(:first)").on("shown.bs.tab", (e) => {
- var id = $(e.target).attr("href").substr(1);
- window.location.hash = id;
-});
+// Variant Picker
+
+const variantPicker = document.getElementById('variant-picker')
+if (variantPicker) {
+ const variantPickerData = JSON.parse(variantPicker.dataset.variantPickerData)
+ ReactDOM.render(
+ <VariantPicker
+ attributes={variantPickerData.attributes}
+ url={variantPicker.dataset.action}
+ variants={variantPickerData.variants}
+ />,
+ variantPicker
+ )
+}
-var hash = window.location.hash;
-$('.nav-tabs a[href="' + hash + '"]').tab('show');
+// Cart quantity form
+
+var $cartLine = $('.cart__line')
+var $total = $('.cart-total')
+$cartLine.each(function() {
+ var $quantityInput = $(this).find('#id_quantity')
+ var cartFormUrl = $(this).find('.form-cart').attr('action')
+ var $formError = $(this).find('.cart__line__quantity-error')
+ var $subtotal = $(this).find('.cart-item-subtotal h3')
+ var $deleteIcon = $(this).find('.cart-item-delete')
+ $(this).on('change', $quantityInput, (e) => {
+ var newQuantity = $quantityInput.val()
+ $.ajax({
+ url: cartFormUrl,
+ method: 'POST',
+ data: {quantity: newQuantity},
+ success: (response) => {
+ $subtotal.html(response.subtotal)
+ $total.html(response.total)
+ $formError.html('')
+ },
+ error: (response) => {
+ var qunatityError = $.parseJSON(response.responseText).error.quantity
+ $formError.html(qunatityError)
+ }
+ })
+ })
+ $deleteIcon.on('click', (e) => {
+ $.ajax({
+ url: cartFormUrl,
+ method: 'POST',
+ data: {quantity: 0},
+ success: (response) => {
+ location.reload()
+ }
+ })
+ })
+})
diff --git a/saleor/static/scss/storefront/components/_cart.scss b/saleor/static/scss/storefront/components/_cart.scss
index 15a895e2e1..5d42eb1b97 100644
--- a/saleor/static/scss/storefront/components/_cart.scss
+++ b/saleor/static/scss/storefront/components/_cart.scss
@@ -35,6 +35,15 @@
}
}
}
+ &__quantity {
+ input {
+ width: 4rem;
+ padding: $global-padding / 2;
+ &:focus {
+ outline: none;
+ }
+ }
+ }
}
&__total {
border-top: 1px solid $gray;
diff --git a/templates/cart/index.html b/templates/cart/index.html
index 67589f8c5d..ae0ee7cfe5 100644
--- a/templates/cart/index.html
+++ b/templates/cart/index.html
@@ -46,25 +46,21 @@
<br><small>{{ line.variant.display_variant }}</small></h3>
</a>
</div>
- <div class="cart-item-amount">
+ <div class="cart__line__quantity col-md-2">
<form role="form" action="{% url "cart:update-line" variant_id=line.variant.pk %}" method="post" class="form-cart">
- <div class="input-group cart-item-quantity js-hidden{% if form.quantity.errors %} has-error{% endif %}">
+ <div class="{% if form.quantity.errors %} has-error{% endif %}">
{{ line.form.quantity }}
- <span class="input-group-btn">
- <button class="btn btn-info" type="submit">{% trans "Update" %}</button>
- </span>
</div>
- {% if line.form.quantity.errors %}
- {% for error in form.quantity.errors %}
- <span class="text-danger">{{ error }}</span>
- {% endfor %}
- {% endif %}
{% csrf_token %}
</form>
+ <small class="cart__line__quantity-error text-danger"></small>
</div>
- <div class="cart-item-subtotal" data-product-id="{{ line.variant.pk }}">
+ <div class="cart-item-subtotal col-md-2" data-product-id="{{ line.variant.pk }}">
<h3>{% gross line.get_total html=True %}</h3>
</div>
+ <div class="col-md-2 cart-item-delete">
+ <img src="{% static 'img/icon_delete.svg' %}">
+ </div>
</div>
</div>
{% endfor %}