Skip to content

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

Add cart subtotal and total update

Paweld8a96b812b4e
diff --git a/Gruntfile.js b/Gruntfile.js
index 26bd295838..8a69ca30de 100644
--- a/Gruntfile.js
+++ b/Gruntfile.js
@@ -3,7 +3,7 @@ module.exports = function(grunt) {
babel: {
dist: {
files: {
- 'saleor/static/js/storefront_fromjsx.js': 'saleor/static/js_src/storefront.jsx'
+ "saleor/static/js/storefront_fromjsx.js": "saleor/static/js_src/storefront.jsx"
},
options: {
stage: 0
diff --git a/saleor/cart/forms.py b/saleor/cart/forms.py
index 120c5c9f4d..6c69e4c0de 100644
--- a/saleor/cart/forms.py
+++ b/saleor/cart/forms.py
@@ -106,7 +106,6 @@ class ReplaceCartLineForm(AddToCartForm):
"""
Replace quantity.
"""
- print repr(self.cart), repr(self.product), repr(self.cleaned_data)
return self.cart.add(self.product, self.cleaned_data['quantity'],
replace=True)
diff --git a/saleor/cart/templates/cart/index.html b/saleor/cart/templates/cart/index.html
index ee62453bb0..241dd36540 100644
--- a/saleor/cart/templates/cart/index.html
+++ b/saleor/cart/templates/cart/index.html
@@ -26,7 +26,7 @@
<tfoot>
<tr>
<td class="col-sm-10 text-right lead" colspan="3">{% trans "Total" %}:
- <td class="col-sm-2 text-right lead"><b>{% gross cart.get_total %}</b></td>
+ <td class="col-sm-2 text-right lead"><b class="cart-total">{% gross cart.get_total %}</b></td>
</tr>
</tfoot>
<tbody>
@@ -59,13 +59,14 @@
{% endif %}
{% csrf_token %}
</form>
+
+ <button type="submit" class="btn btn-link btn-sm cart-item-remove">
+ <span class="text-muted">{% trans "Remove from cart" %}</span>
+ </button>
</div>
- <button type="submit" class="btn btn-link btn-sm cart-item-remove">
- <span class="text-muted">{% trans "Remove from cart" %}</span>
- </button>
</td>
<td class="text-right">{% gross line.get_price_per_item %}</td>
- <td class="text-right cart-item-subtotal">
+ <td class="text-right cart-item-subtotal" data-product-id="{{ line.product.pk }}">
{% gross line.get_total %}
</td>
</tr>
diff --git a/saleor/cart/views.py b/saleor/cart/views.py
index 430f150332..e56f720047 100755
--- a/saleor/cart/views.py
+++ b/saleor/cart/views.py
@@ -1,7 +1,8 @@
from __future__ import unicode_literals
+from babeldjango.templatetags.babel import currencyfmt
from django.contrib import messages
-from django.http import HttpResponse, JsonResponse
+from django.http import JsonResponse
from django.shortcuts import redirect
from django.template.response import TemplateResponse
from django.utils.translation import ugettext as _
@@ -32,7 +33,13 @@ def index(request, product_id=None):
if form.is_valid():
form.save()
if request.is_ajax():
- return HttpResponse('')
+ response = {'product_id': line.product.pk,
+ 'subtotal': currencyfmt(line.get_total().gross,
+ line.get_total().currency), 'total': 0}
+ if cart:
+ response['total'] = currencyfmt(
+ cart.get_total().gross, cart.get_total().currency)
+ return JsonResponse(response)
return redirect('cart:index')
elif data is not None:
if request.is_ajax():
diff --git a/saleor/static/js_src/storefront.jsx b/saleor/static/js_src/storefront.jsx
index c2ff0f49fa..ec033e57d3 100644
--- a/saleor/static/js_src/storefront.jsx
+++ b/saleor/static/js_src/storefront.jsx
@@ -61,7 +61,15 @@ class CartItemAmount extends React.Component {
this.setState({renderSelect: true});
}
},
- success: () => {
+ success: (response) => {
+ if (!quantity) {
+ if (!response.total) {
+ location.reload();
+ }
+ $(React.findDOMNode(this)).parents("tr").fadeOut(function() {
+ $(this).remove();
+ });
+ }
this.setState({result: "success", lastSavedValue: quantity});
setTimeout(() => {
this.setState({result: null});
@@ -73,6 +81,10 @@ class CartItemAmount extends React.Component {
});
}
+ removeFromCart() {
+ this.sendQuantity(0);
+ }
+
render() {
let classNames = React.addons.classSet({
[this.props.className]: true,
@@ -102,6 +114,9 @@ class CartItemAmount extends React.Component {
{this.state.renderSelect ? select : input}
{this.state.sending && !(this.state.result == "error")? <i className="fa fa-circle-o-notch fa-spin"></i> : ""}
{this.state.result == "error" ? <span className="error text-danger">{this.state.error}</span> : ""}
+ <button type="submit" className="btn btn-link btn-sm cart-item-remove" onClick={this.removeFromCart.bind(this)}>
+ <span className="text-muted">Remove from cart</span>
+ </button>
</div>;
}
}
@@ -114,6 +129,26 @@ class CartItemAmountOption extends React.Component {
};
}
+class CartItemSubtotal extends React.Component {
+ state = {
+ value: this.props.value
+ };
+
+ render() {
+ return <span>{this.state.value}</span>;
+ }
+}
+
+class CartTotal extends React.Component {
+ state = {
+ value: this.props.value
+ };
+
+ render() {
+ return <b>{this.state.value}</b>;
+ }
+}
+
var textInput = [];
var options = [1,2,3,4,5,6,7,8,9,10];
var csrftoken = $.cookie('csrftoken');
@@ -151,6 +186,28 @@ $(".cart-item-amount").each(function(index) {
React.render(<CartItemAmount {...props} />, this);
});
+var $cartTotal = $(".cart-total");
+var cartTotalValue = $cartTotal.text();
+var cartTotal = React.render(<CartTotal value={cartTotalValue} />, $(".cart-total")[0]);
+
+var cartSubtotals = [];
+$(".cart-item-subtotal").each(function() {
+ var productId = $(this).data("product-id");
+ var props = {
+ productId: productId,
+ value: $(this).text()
+ };
+ cartSubtotals[productId] = React.render(<CartItemSubtotal {...props} />, this);
+});
+
+$(document).on("ajaxComplete", function(event, response) {
+ var json = response.responseJSON;
+ if (json.product_id && $cartTotal.length) {
+ cartSubtotals[json.product_id].setState({value: json.subtotal});
+ cartTotal.setState({value: json.total});
+ }
+});
+
class FormShippingToggler extends React.Component {
state = {
value: true
diff --git a/saleor/static/scss/_app.scss b/saleor/static/scss/_app.scss
index 0b83e9d66b..9f026f777c 100644
--- a/saleor/static/scss/_app.scss
+++ b/saleor/static/scss/_app.scss
@@ -123,10 +123,6 @@ $icon-font-path: "../fonts/";
}
}
- &-subtotal {
- position: relative;
- }
-
&-remove {
padding-left: 0;
}