matthiask / plata

Plata - the lean and mean Django-based Shop
https://plata-django-shop.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
197 stars 63 forks source link

Automatic discounts -- f.e. to implement discounts for predefined groups of users #19

Open matthiask opened 12 years ago

matthiask commented 12 years ago

... without them having to enter a discount code.

meric commented 11 years ago

How about changing get_price(self, currency=None, orderitem=None) to get_price(self, user=AnonymousUser, currency=None, orderitem=None).

A default price for anonymous user, as well as a different price for specific users if necessary.

matthiask commented 11 years ago

Yes, I think that's probably the way to go. I'll probably have to solve this problem too in an upcoming shop project of ours.

matthiask commented 11 years ago

On second thought you're getting the order model already which as a FK to the user model.

You're getting the current user instance by accessing orderitem.order.user. Is that sufficient for your use case?

Soaa- commented 11 years ago

Getting the instance from orderitem.order.user can only be used in the cart and not the actual product display, am I right? Is there currently a way to display a user's prices throughout the shop?

matthiask commented 11 years ago

Unfortunately that's not easily possible currently. You could simulate this feature by creating an OrderItem yourself and passing it to the handle_orderitem methods.

You still won't be able to display prices properly for more complicated discount rules. If you have discount rules which span several orderitem (for example by having these two items in the cart you get a 10% discount on each of them).

A different thought: I'm working on a shop currently where the shop admin must be able to assign different (lower) prices for specific users. You can easily specify two price models: One for specific users (coming with a foreign key to the User model) and another for everyone else.

Does that help?

Soaa- commented 11 years ago

Hum... I'm going to try writing Product model like @meric suggested, that overrides the get_price() to take an extra argument user. To display the price throughout the shop, I'd create a filter that takes a Product object and an argument user, which will return the price for the selected user.

My logic is that if we add an argument user to get_price(), we can easily set up pricing rules for both specific users and users in a group. The PriceBase model then doesn't need any modifications, and rules can be easily created by the programmer.

Maybe I'm just repeating what you had in mind already, in which case we'd be in agreement, which is good. :D

matthiask commented 11 years ago

Yes, I agree. You'll probably have to write code similar to this:

def get_price(self, currency=None, orderitem=None, user=None):
    if user is None:
        if orderitem and orderitem.order.user:
            # Calculate price for the given order
            user = orderitem.order.user
    else:
        # Calculate price for displaying in the shop

The reason is that you cannot control all invocations of get_price. You'll have to implement both cases, getting the user and getting the order item.