Skip to content

Instantly share code, notes, and snippets.

@joshcartme
Created October 24, 2012 04:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joshcartme/3943826 to your computer and use it in GitHub Desktop.
Save joshcartme/3943826 to your computer and use it in GitHub Desktop.
cartridge order setup which adds in tax total
def custom_order_setup(self, request):
"""
Set order fields that are stored in the session, item_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.
Adds tax to the order total.
"""
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
if Decimal(str(request.session.get('tax_total'))) is not None:
self.tax_total = Decimal(str(request.session.get('tax_total')))
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 = custom_order_setup
@kenbolton
Copy link

I see how this differs from https://github.com/kenbolton/cartridge-tax/blob/master/cartridge_tax/models.py, but I am not sure why you need to get the tax_total from the session. Can you clarify that for me?

@joshcartme
Copy link
Author

My understanding is that at this point (tax_order_handler hasn't been called yet) tax_total only exists in the session. I could be wrong about that but I'm pulling it out of the session for the same reason that tax_order_handler currently pulls it out of the session.

I'm not using the most current version of cartridge tax which could have something to do with it, but how would self.tax_total be set before the order setup is called? I set the site I'm working on back to using your custom setup and tax_total was always none. Isn't self.tax_total not set until the tax_order_handler? And if it is set before that, wouldn't tax_total be added to the order total twice, once here and once in tax_order_handler?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment