Open matthiask opened 12 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.
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.
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?
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?
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?
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
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.
... without them having to enter a discount code.