#Discount system in Django

26 messages · Page 1 of 1 (latest)

odd moth
#

Hi everyone, I need help with implementing a discount system for my e-commerce store. Here's the flow:

Cart Page: Users can update/delete items or add a coupon. It is stored in redis.

Checkout: An order summary is displayed before users place the order.

Place Order: Data is persisted to the database, and users see the final order summary before proceeding to payment.

Discounts I want to implement:

  1. First-time Purchase: Automatically apply a 10% discount on the first order.

  2. Seasonal Discounts: Discounts on specific products (e.g., Black Friday – 10%, 20% per product).

The methods I've tried are too complex, fragile, and break my code. Any suggestions on how to implement this efficiently?

stone bone
#

hi
it'd be nice if you showed some code

off the top of my head, since you probably have the user object when handling the shop, you could add a boolean field to the user model, which tells you if the user has bought something before or not, and change it's value when they buy for the first time

or you could check if they have shopped before by quering the foreign key to the shop record model, but i think that's extra overhead

#

.
.
.
for the second one, you can simply add a discount field to your product model

and calculate the discount when shop is happening

or you could have a global setting for discounts if everything is gonna have the same amount of discount
and read that

#

.
.
you could also model the product model like this

original price
discount amount
discounted price

and calculate the discounted price on each save to the model
so you don't have to calculate it each time someone buys something

#

tho this doesn't apply to coupons and they need to be calculated separately

#

.
i would move coupons out of cart section and in placing the order section
.

#

and i would store the cart in a persistent database to be able to give persistent support to the user

tho there is a benefit to storing it in session

you might even do both (session for unauthenticated users, db for authenticated)

#

.
also, may i interest you in using valkey instead of redis? BlobCatHeart

odd moth
#

Alright, straight to implementation.

#

Valkey would be overlycomplex cos I am still learning.

stone bone
#

tho take my notes with a bit of caution since it's 4:43 am 🙃

#

if you are learning use whatever is more comfortable to you.

i suggested valkey since i thought you are doing production work and redis is not free.

odd moth
lucid spruce
lucid spruce
stone bone
#

the question was about discounting on the first time,
but counter should work too

lucid spruce
#

Yes, it will solve that particular problem, but boolean field would serve only one purpose and then it would be not of much help. That' why, I thought count could be something that tells more about the user shopping.

stone bone
#

fair enough

odd moth
#

Let me explain the issue I’m currently facing:

I added a first_purchase flag to the Profile model to ensure a user can only receive the first purchase discount once. I’m using Paystack (a popular payment gateway in Nigeria) webhook to update this flag to False after a successful payment.

The Problem

In the admin panel, I have two views:

  1. admin_order_detail

  2. admin_order_pdf

The problem arises because the data is mutated (i.e., the first_purchase flag changes to False after the payment is successful). As a result, the condition in my template fails, so the discount is no longer displayed in these views.

What I’ve Tried

I’ve brainstormed several solutions, including using ChatGPT to explore options, but most of them risk breaking my code or making it fragile.

My Current Approach

I’m still learning and experimenting with different ideas. I’d like to hear your thoughts on how to fix this issue in a way that maintains clean, reliable, and maintainable code.

stone bone
# odd moth I don't understand

having a filed at your user model
shop_count = models.PositiveIntegerField(..., default=0)

so if it's 0, user hasn't bought anything

#

if not, you know how many time they have shopped

odd moth
odd moth
odd moth
stone bone