Skip to content

Instantly share code, notes, and snippets.

@kenbolton
Forked from joshcartme/gist:3943826
Created October 25, 2012 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenbolton/3953717 to your computer and use it in GitHub Desktop.
Save kenbolton/3953717 to your computer and use it in GitHub Desktop.
cartridge order setup which adds in tax total
from decimal import Decimal
from cartridge.shop.models import Order, SelectedProduct
def setup(self, request):
"""
Set order fields that are stored in the session, item_total, tax_total,
and total based on the given cart, and copy the cart items to the order.
Called in the final step of the checkout process prior to the payment
handler being called.
"""
self.key = request.session.session_key
self.user_id = request.user.id
for field in self.session_fields:
if field in request.session:
setattr(self, field, request.session[field])
self.total = self.item_total = request.cart.total_price()
if self.shipping_total is not None:
self.shipping_total = Decimal(str(self.shipping_total))
self.total += self.shipping_total
if self.discount_total is not None:
self.total -= self.discount_total
self.tax_total = Decimal(str(request.session.get('tax_total')))
if self.tax_total is not None:
self.total += self.tax_total
self.save() # We need an ID before we can add related items.
for item in request.cart:
product_fields = [f.name for f in SelectedProduct._meta.fields]
item = dict([(f, getattr(item, f)) for f in product_fields])
self.items.create(**item)
Order.setup = setup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment