Skip to content

Commit

Permalink
Fix bug with stripe minimum order.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomik-z-cech committed Mar 5, 2024
1 parent e7e10ff commit e4af873
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 20 deletions.
1 change: 0 additions & 1 deletion checkout/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,3 @@ def _generate_order_number(self):
Generate a random, unique order number using UUID
"""
return uuid.uuid4().hex.upper()

41 changes: 26 additions & 15 deletions checkout/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ def get(self, request, *args, **kwargs):
if subtotal == 0:
messages.error(request, "Can't proceed to checkout with empty Vault")
return redirect('shop', category_pk = 0)
elif subtotal < postage_settings.minimum_order:
messages.error(request, f"Can't proceed to checkout. The minimum order value is {postage_settings.minimum_order} €.")
return redirect('shop', category_pk = 0)
else:
if subtotal < postage_settings.free_postage:
standard_delivery_cost = round((float(postage_settings.standard_delivery) * subtotal / 100), 2)
Expand Down Expand Up @@ -127,21 +130,29 @@ def post(self, request, *args, **kwargs):
if not stripe_public_key:
messages.error(request, 'Stripe public key missing.')
if request.POST.get('payment-checker') == 'true':
print('payment-processed')
return render(
request,
self.success_name,
{
"order_form": order_form,
"standard_delivery_cost": standard_delivery_cost,
"express_delivery_cost": express_delivery_cost,
"current_voucher": current_voucher,
"total": total,
"selected_delivery_cost": selected_delivery_cost,
"stripe_public_key": stripe_public_key,
"client_secret": intent.client_secret,
}
)
if order_form.is_valid():
# Reset any voucher in use
current_voucher = [False, '', 0, 0]
if 'save-details' in request.POST:
logged_userprofile = request.user.userprofile
logged_userprofile.first_name = order_form.cleaned_data['first_name']
logged_userprofile.last_name = order_form.cleaned_data['last_name']
logged_userprofile.phone_number = order_form.cleaned_data['phone_number']
logged_userprofile.address_1 = order_form.cleaned_data['address_1']
logged_userprofile.address_2 = order_form.cleaned_data['address_2']
logged_userprofile.city = order_form.cleaned_data['city']
logged_userprofile.county = order_form.cleaned_data['county']
logged_userprofile.post_code = order_form.cleaned_data['post_code']
logged_userprofile.country = order_form.cleaned_data['country']
logged_userprofile.save()
# Get final content of session Vault
request.session['current_voucher'] = current_voucher
final_vault = request.session.get('vault', [])
print(final_vault)
return render(
request,
self.success_name,
)
return render(
request,
self.template_name,
Expand Down
5 changes: 5 additions & 0 deletions owner/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ def __init__(self, *args, **kwargs):
'step': '0.05',
'min': '1',
})
self.fields['minimum_order'].widget.attrs.update({
'placeholder': 'Minimum order € (required)',
'step': '1',
'min': '1',
})

class VoucherForm(forms.ModelForm):
"""
Expand Down
50 changes: 50 additions & 0 deletions owner/migrations/0022_postagesettings_minimum_order_and_more.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Generated by Django 4.2.7 on 2024-03-05 10:11

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("owner", "0021_alter_voucher_voucher_code"),
]

operations = [
migrations.AddField(
model_name="postagesettings",
name="minimum_order",
field=models.DecimalField(
decimal_places=2,
default=5,
help_text="Amount in € that VAULT needs to be over for processing the order",
max_digits=5,
),
preserve_default=False,
),
migrations.AlterField(
model_name="postagesettings",
name="express_delivery",
field=models.DecimalField(
decimal_places=2,
help_text="Cost of express delivery - % of VAULT ",
max_digits=5,
),
),
migrations.AlterField(
model_name="postagesettings",
name="free_postage",
field=models.DecimalField(
decimal_places=2,
help_text="Amount in € that VAULT needs to be over for free delivery",
max_digits=5,
),
),
migrations.AlterField(
model_name="postagesettings",
name="standard_delivery",
field=models.DecimalField(
decimal_places=2,
help_text="Cost of standard delivery - % of VAULT ",
max_digits=5,
),
),
]
7 changes: 4 additions & 3 deletions owner/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ def __str__(self):


class PostageSettings(models.Model):
free_postage = models.DecimalField(max_digits=5, decimal_places=2, blank=False, help_text='Amount in € that TOTAL needs to be over for free delivery')
standard_delivery = models.DecimalField(max_digits=5, decimal_places=2, blank=False, help_text='Cost of standard delivery - % of TOTAL')
express_delivery = models.DecimalField(max_digits=5, decimal_places=2, blank=False, help_text='Cost of express delivery - % of TOTAL')
free_postage = models.DecimalField(max_digits=5, decimal_places=2, blank=False, help_text='Amount in € that VAULT needs to be over for free delivery')
standard_delivery = models.DecimalField(max_digits=5, decimal_places=2, blank=False, help_text='Cost of standard delivery - % of VAULT ')
express_delivery = models.DecimalField(max_digits=5, decimal_places=2, blank=False, help_text='Cost of express delivery - % of VAULT ')
minimum_order = models.DecimalField(max_digits=5, decimal_places=2, blank=False, help_text='Amount in € that VAULT needs to be over for processing the order')

def __int__(self):
return self.free_postage
Expand Down
2 changes: 1 addition & 1 deletion static/js/easter_egg.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ $(document).keydown(function(event) {
// Check if the key sequence matches the specified string
if (secretCode.includes('ilovetocode')) {
// Show an alert window
alert('Great ! Enjoy 75% discount with code DJANGO75 !');
alert('Great ! Enjoy 75% discount with code DJANGO75 valid through 2024 !');
// Reset the key sequence for the next input
secretCode = '';
}
Expand Down

0 comments on commit e4af873

Please sign in to comment.