Calculating with products price depending on users attribute.

21 views
Skip to first unread message

Michal Raška

unread,
Jan 7, 2014, 2:13:05 PM1/7/14
to djang...@googlegroups.com
Hi,
I am new to django shop and now I am solving such problem how to calculate carts total price with products price depending on users attribute (is_wholesale).
I have 2 prices in product (retail and wholesale price). And I need calculate with one of them depending on user.is_wholesale attribute.

Currently I modified CartItem model so:

class CartItem(BaseCartItem):

    class Meta(object):
        verbose_name = _("Cart item")
        verbose_name_plural = _("Cart items")

    def get_unit_price(self):

        if self.cart.user and self.cart.user.is_wholesale:
            return self.product.get_unit_wholesale_price()

        return self.product.get_unit_retail_price()

    def update(self, request):
        self.extra_price_fields = []  # Reset the price fields
        self.line_subtotal = self.get_unit_price() * self.quantity
        self.current_total = self.line_subtotal

        for modifier in cart_modifiers_pool.get_modifiers_list():
            # We now loop over every registered price modifier,
            # most of them will simply add a field to extra_payment_fields
            modifier.process_cart_item(self, request)

        self.line_total = self.current_total
        return self.line_total

But I need similar logic to display price on product detail site and I hate duplicity. :)
Would be better to pass user instance to product.get_price method and make decision there? Or something else? Create some helper to make decision and pass to it user and product?

What do you suggest? Thanks a lot!
Reply all
Reply to author
Forward
0 new messages