Created
September 23, 2011 16:45
-
-
Save eserge/1237838 to your computer and use it in GitHub Desktop.
Example cart modifier for django-shop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import decimal | |
from django.conf import settings | |
from shop.cart.cart_modifiers_base import BaseCartModifier | |
class FixedShippingCosts(BaseCartModifier): | |
# """ | |
# This will add a fixed amount of money for shipping costs. | |
# """ | |
def process_cart(self, cart, state): | |
shipping_cost = decimal.Decimal(settings.SHOP_SHIPPING_FLAT_RATE) | |
cart.current_total = cart.current_total + shipping_cost | |
cart.extra_price_fields.append( | |
('Shipping costs', shipping_cost)) | |
return cart | |
class FixedTaxRate(BaseCartModifier): | |
def process_cart(self, cart, state): | |
""" | |
This will be called once per Cart, after every line item was treated | |
The subtotal for the cart is already known, but the Total is 0. | |
Like for the line items, total is expected to be calculated in the cart's | |
update() method. | |
Line items should be complete by now, so all of their fields are accessible | |
Subtotal is accessible, but total is still 0.0. Overrides are expected to | |
update cart.current_total. | |
The state parameter is only used to let implementations store temporary | |
information to pass between cart_item_modifers and cart_modifiers | |
""" | |
field = self.get_extra_cart_price_field(cart) | |
if field != None: | |
price = field[1] | |
cart.current_total = cart.current_total + price | |
cart.extra_price_fields.append(field) | |
taxes = cart.current_total * decimal.Decimal('0.19') | |
to_append = ('Taxes total', taxes) | |
cart.extra_price_fields.append(to_append) | |
cart.current_total = cart.current_total + taxes | |
return cart | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment