English 中文(简体)
Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the extra free items to their order after they checkout. Of course, it d be nice to automate this entire process.

I ve been mulling over a few ideas, mainly creating a Discount model (I m using Django in this case, but this is more of a logic question) and having that model have a variety of flags and product lists so I could create an instance like so:

       description="Get one free pair of bands when you buy two pairs of shoes.",
       valid_products=[BigProductA, BigProductB],
       received_products=[FreebieProductA, FreebieProductB],
       special_in_intervals=2, # Whenever the user buys 2, give one for free

This logic kind of works. I can then take a look at what is in their cart and test against the existing Discounts in the model and see if they apply for anything. The biggest problem with this is it can get very messy especially if you have multiple specials going on and I just don t see it working out too well.

Unfortunately, that s really my best idea for this right now. So, I come to ask you guys: What do you think is the best approach for this? I m not looking for code, just some ideas of logic and ways to do this. :)

Thanks in advance!


Welcome to hell. Stay a while. ;) Ahem.

Discounts are a mess, so it s not surprising that you feel tainted by having to work with them. From a design point of view, the testing should be part of the Discount instance, i.e. there should be an appliesTo(cart) method and an apply(cart) method. The first tells you whether a discount applies, the second one actually applies the discount. I suggest that the apply() method doesn t change the "user part" of the cart but instead modifies extra fields, so you can easily reset the cart (drop all discounts) and run the process again.

This way, you can cleanly implement the two types of discounts that appear most often: "Get X for free, when buying Y" and "get a rebate of X% if you buy for Y $$$". Since you don t change the original figures, you can easily apply multiple discounts and rebates.

I also suggest to back this up with a whole lot of unit tests to make sure the whole thing behaves as you expect. Otherwise the next discount might be your last. :)


I don t quiet get the question - but if you select DISTINCT (I m writing "pseudo logic" in SQL) all free items that match the items in the car , and then if you wish to give only one or n of them - SELECT TOP(n) DISTINCT from tblFREE where freebeid in (select freebdid from tbl itemsfreebe where items in (Select Items from CART where **** Freebe givaway LOGIC***))

freebe giveaway logic is the generic placeholder that should always evaluate for true or false:

like where (select count(*) from cart >2) so if the logic works - you ll get items in the list, and if not - you ll get nothing.

you can move this logic to your code and run only the first part of the "query" in the DB...

logic can be used with AND or OR with other logics....

once the user accept the offer - you add the list to the cart, and should rais a flag that the discount/freebee was applied - so it won t happen twice...

I wonder what does it means that it easier to SQL it than to say it :-)

I hope that targets your question...

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...