def update(self, request): """ This should be called whenever anything is changed in the cart (added or removed). It will loop on all line items in the cart, and call all the price modifiers on each row. After doing this, it will compute and update the order's total and subtotal fields, along with any payment field added along the way by modifiers. Note that theses added fields are not stored - we actually want to reflect rebate and tax changes on the *cart* items, but we don't want that for the order items (since they are legally binding after the "purchase" button was pressed) """ from shop.models import CartItem, Product # This is a ghetto "select_related" for polymorphic models. items = CartItem.objects.filter(cart=self).order_by('pk') product_ids = [item.product_id for item in items] products = Product.objects.filter(pk__in=product_ids) products_dict = dict([(p.pk, p) for p in products]) self.extra_price_fields = [] # Reset the price fields self.subtotal_price = Decimal('0.0') # Reset the subtotal # The request object holds extra information in a dict named 'cart_modifier_state'. # Cart modifiers can use this dict to pass arbitrary data from and to each other. if not hasattr(request, 'cart_modifier_state'): setattr(request, 'cart_modifier_state', {}) # This calls all the pre_process_cart methods (if any), before the cart # is processed. This allows for data collection on the cart for # example) for modifier in cart_modifiers_pool.get_modifiers_list(): modifier.pre_process_cart(self, request) for item in items: # For each CartItem (order line)... # This is still the ghetto select_related item.product = products_dict[item.product_id] self.subtotal_price = self.subtotal_price + item.update(request) self.current_total = self.subtotal_price # Now we have to iterate over the registered modifiers again # (unfortunately) to pass them the whole Order this time for modifier in cart_modifiers_pool.get_modifiers_list(): modifier.process_cart(self, request) self.total_price = self.current_total # This calls the post_process_cart method from cart modifiers, if any. # It allows for a last bit of processing on the "finished" cart, before # it is displayed for modifier in cart_modifiers_pool.get_modifiers_list(): modifier.post_process_cart(self, request) # Cache updated cart items self._updated_cart_items = items
def update(self, state=None): """ This should be called whenever anything is changed in the cart (added or removed). It will loop on all line items in the cart, and call all the price modifiers on each row. After doing this, it will compute and update the order's total and subtotal fields, along with any payment field added along the way by modifiers. Note that theses added fields are not stored - we actually want to reflect rebate and tax changes on the *cart* items, but we don't want that for the order items (since they are legally binding after the "purchase" button was pressed) """ from shop.models import CartItem, Product # This is a ghetto "select_related" for polymorphic models. items = CartItem.objects.filter(cart=self) product_ids = [item.product_id for item in items] products = Product.objects.filter(id__in=product_ids) products_dict = dict([(p.id, p) for p in products]) self.extra_price_fields = [] # Reset the price fields self.subtotal_price = Decimal("0.0") # Reset the subtotal # This will hold extra information that cart modifiers might want to pass # to each other if state == None: state = {} # This calls all the pre_process_cart methods (if any), before the cart # is processed. This allows for data collection on the cart for example) for modifier in cart_modifiers_pool.get_modifiers_list(): modifier.pre_process_cart(self, state) for item in items: # For each OrderItem (order line)... item.product = products_dict[item.product_id] # This is still the ghetto select_related self.subtotal_price = self.subtotal_price + item.update(state) self.current_total = self.subtotal_price # Now we have to iterate over the registered modifiers again (unfortunately) # to pass them the whole Order this time for modifier in cart_modifiers_pool.get_modifiers_list(): modifier.process_cart(self, state) self.total_price = self.current_total # This calls the post_process_cart method from cart modifiers, if any. # It allows for a last bit of processing on the "finished" cart, before # it is displayed for modifier in cart_modifiers_pool.get_modifiers_list(): modifier.post_process_cart(self, state)
def update(self): """ This should be called whenever anything is changed in the cart (added or removed) It will loop on all line items in the cart, and call all the price modifiers on each row. After doing this, it will compute and update the order's total and subtotal fields, along with any payment field added along the way by modifiers. Note that theses added fields are not stored - we actually want to reflect rebate and tax changes on the *cart* items, but we don't want that for the order items (since they are legally binding after the "purchase" button was pressed) """ items = CartItem.objects.filter(cart=self) self.subtotal_price = Decimal('0.0') # Reset the subtotal for item in items: # For each OrderItem (order line)... self.subtotal_price = self.subtotal_price + item.update() item.save() # Now we have to iterate over the registered modifiers again (unfortunately) # to pass them the whole Order this time for modifier in cart_modifiers_pool.get_modifiers_list(): modifier.process_cart(self) self.total_price = self.subtotal_price # Like for line items, most of the modifiers will simply add a field # to extra_price_fields, let's update the total with them for label, value in self.extra_price_fields: self.total_price = self.total_price + value
def update(self): ''' This should be called whenever anything is changed in the cart (added or removed) It will loop on all line items in the cart, and call all the price modifiers on each row. After doing this, it will compute and update the order's total and subtotal fields, along with any payment field added along the way by modifiers. Note that theses added fields are not stored - we actually want to reflect rebate and tax changes on the *cart* items, but we don't want that for the order items (since they are legally binding after the "purchase" button was pressed) ''' items = list(CartItem.objects.filter(cart=self)) # force query to "cache" it self.subtotal_price = Decimal('0.0') # Reset the subtotal for item in items: # For each OrderItem (order line)... item.line_subtotal = item.product.unit_price * item.quantity item.line_total = item.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 item = modifier.process_cart_item(item) for value in item.extra_price_fields.itervalues(): item.line_total = item.line_total + value # Let's update the Order's subtotal with this line's total while # we're at it self.subtotal_price = self.subtotal_price + item.line_total item.save() # Now we have to iterate over the registered modifiers again (unfortunately) # to pass them the whole Order this time for modifier in cart_modifiers_pool.get_modifiers_list(): modifier.process_cart(self) self.total_price = self.subtotal_price # Like for line items, most of the modifiers will simply add a field # to extra_price_fields, let's update the total with them for value in self.extra_price_fields.itervalues(): self.total_price = self.total_price + value self.save()
def update(self, state): self.extra_price_fields = [] # Reset the price fields self.line_subtotal = self.product.get_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, state) self.line_total = self.current_total return self.line_total
def update(self): self.line_subtotal = self.product.get_price() * self.quantity self.line_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) for value in self.extra_price_fields.itervalues(): self.line_total = self.line_total + value return self.line_total
def update(self, request): self.extra_price_fields = [] # Reset the price fields self.line_subtotal = self.product.get_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
def update(self): self.line_subtotal = self.product.get_price() * self.quantity self.line_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) for label, value in self.extra_price_fields: self.line_total = self.line_total + value return self.line_total
def update(self): """ This should be called whenever anything is changed in the cart (added or removed). It will loop on all line items in the cart, and call all the price modifiers on each row. After doing this, it will compute and update the order's total and subtotal fields, along with any payment field added along the way by modifiers. Note that theses added fields are not stored - we actually want to reflect rebate and tax changes on the *cart* items, but we don't want that for the order items (since they are legally binding after the "purchase" button was pressed) """ from shop.models import CartItem, Product # This is a ghetto "select_related" for polymorphic models. 2 queries! items = CartItem.objects.filter(cart=self) product_ids = [item.product_id for item in items] products = Product.objects.filter(id__in=product_ids) products_dict = dict([(p.id, p) for p in products]) self.extra_price_fields = [] # Reset the price fields self.subtotal_price = Decimal('0.0') # Reset the subtotal for item in items: # For each OrderItem (order line)... item.product = products_dict[item.product_id] #This is still the ghetto select_related self.subtotal_price = self.subtotal_price + item.update() item.save() self.current_total = self.subtotal_price # Now we have to iterate over the registered modifiers again (unfortunately) # to pass them the whole Order this time for modifier in cart_modifiers_pool.get_modifiers_list(): modifier.process_cart(self) self.total_price = self.current_total