add delete operation to cart
This commit is contained in:
parent
935c97de46
commit
e2fce385da
|
@ -69,7 +69,7 @@ class AddToCartForm(forms.Form):
|
|||
class CartForm(forms.Form):
|
||||
amount_form = forms.FloatField(
|
||||
label='pce',
|
||||
help_text='Enter a Value between 1 and 99.')
|
||||
)
|
||||
|
||||
|
||||
class CheckoutForm(forms.Form):
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
{{ amount_form.as_p }}
|
||||
<input type="hidden" value="{{ cart_position.article.id }}" name="article_id">
|
||||
<input type="submit" value="change" />
|
||||
<input type="submit" value="delete" name="delete" />
|
||||
{% csrf_token %}
|
||||
</form>
|
||||
<!-- {{ article.amount }} -->
|
||||
|
|
|
@ -77,21 +77,24 @@ def restrict_cart_to_one_article(user_name, article_id, amount, operation):
|
|||
print('restrict_cart_to_one_article cart_id:', cart_id)
|
||||
# check if the article is existent in cart already:
|
||||
try:
|
||||
article_amount = CartPosition.objects.get(
|
||||
article = CartPosition.objects.get(
|
||||
article=article_id)
|
||||
if operation == 'add':
|
||||
new_amount = article_amount.amount + amount
|
||||
new_amount = article.amount + amount
|
||||
print('restrict_cart_to_one_article add new_amount:', new_amount,
|
||||
'article_id', article_id)
|
||||
if operation == 'replace':
|
||||
new_amount = amount
|
||||
new_amount = amount # ref two times check later !!
|
||||
print('restrict_cart_to_one_article replace:', new_amount,
|
||||
'article_id', article_id)
|
||||
# if article is in cart already update amount:
|
||||
cart_position = CartPosition.objects.filter(
|
||||
id=article_id).update(
|
||||
amount=new_amount
|
||||
)
|
||||
# if article is in cart already update amount:
|
||||
cart_position = CartPosition.objects.filter(
|
||||
id=article_id).update(
|
||||
amount=new_amount
|
||||
)
|
||||
if operation == 'delete':
|
||||
article.delete()
|
||||
print('restrict_cart_to_one_article delete article_id:', article_id)
|
||||
except Exception as e:
|
||||
print('restrict_cart_to_one_article except: ', e)
|
||||
# if the article is not in cart yet add full item:
|
||||
|
@ -258,6 +261,21 @@ def cart(request):
|
|||
amount,
|
||||
operation
|
||||
)
|
||||
# here we react to a change of amount per item in the Cart:
|
||||
if 'delete' in request.POST:
|
||||
print('delete yes delete post')
|
||||
delete = CartForm(request.POST)
|
||||
if delete.is_valid():
|
||||
amount = delete.cleaned_data['amount_form']
|
||||
article_id = request.POST.get('article_id')
|
||||
amount = 1
|
||||
operation = 'delete'
|
||||
restrict_cart_to_one_article(
|
||||
user_name,
|
||||
article_id,
|
||||
amount,
|
||||
operation
|
||||
)
|
||||
|
||||
if 'checkout' in request.POST:
|
||||
print('checkout')
|
||||
|
|
Loading…
Reference in New Issue