Check stock when incrementing orderproduct quantity.

This commit is contained in:
Víðir Valberg Guðmundsson 2019-03-27 20:25:06 +01:00
parent 8a5b2e5ed1
commit 101cb2db63
2 changed files with 20 additions and 2 deletions

View file

@ -55,9 +55,13 @@ Shop | {{ block.super }}
<a href="{% url 'shop:product_detail' slug=product.slug %}"> <a href="{% url 'shop:product_detail' slug=product.slug %}">
{{ product.name }} {{ product.name }}
</a> </a>
{% if product.stock_amount and product.left_in_stock <= 10 %} {% if product.stock_amount %}
<div class="label label-danger"> <div class="label label-danger">
Only {{ product.left_in_stock }} left! {% if product.left_in_stock == 0 %}
Sold out!
{% elif product.left_in_stock <= 10 %}
Only {{ product.left_in_stock }} left!
{% endif %}
</div> </div>
{% endif %} {% endif %}
</td> </td>

View file

@ -335,6 +335,20 @@ class OrderDetailView(LoginRequiredMixin, EnsureUserOwnsOrderMixin, EnsureOrderH
order_product_id = str(order_product.pk) order_product_id = str(order_product.pk)
if order_product_id in request.POST: if order_product_id in request.POST:
new_quantity = int(request.POST.get(order_product_id)) new_quantity = int(request.POST.get(order_product_id))
if order_product.quantity < new_quantity:
# We are incrementing and thus need to check stock
incrementing_by = new_quantity - order_product.quantity
if incrementing_by > order_product.product.left_in_stock:
messages.error(
request,
"Sadly we only have {} '{}' left in stock.".format(
order_product.product.left_in_stock,
order_product.product.name,
)
)
return super(OrderDetailView, self).get(request, *args, **kwargs)
order_product.quantity = new_quantity order_product.quantity = new_quantity
order_product.save() order_product.save()
order.customer_comment = request.POST.get('customer_comment') or '' order.customer_comment = request.POST.get('customer_comment') or ''