|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+===========================
|
|
|
2
|
+How to apply tax exemptions
|
|
|
3
|
+===========================
|
|
|
4
|
+
|
|
|
5
|
+Problem
|
|
|
6
|
+=======
|
|
|
7
|
+The tax a customer pays depends on the shipping address of his/her
|
|
|
8
|
+order.
|
|
|
9
|
+
|
|
|
10
|
+Solution
|
|
|
11
|
+========
|
|
|
12
|
+Use custom basket middleware to set the tax status of the basket.
|
|
|
13
|
+
|
|
|
14
|
+The default Oscar basket middleware is::
|
|
|
15
|
+
|
|
|
16
|
+ 'oscar.apps.basket.middleware.BasketMiddleware'
|
|
|
17
|
+
|
|
|
18
|
+To alter the tax behaviour, replace this class with one within your own project
|
|
|
19
|
+that subclasses Oscar's and extends the ``get_basket`` method. For example, use
|
|
|
20
|
+something like::
|
|
|
21
|
+
|
|
|
22
|
+ from oscar.apps.basket.middleware import BasketMiddleware
|
|
|
23
|
+ from oscar.apps.checkout.utils import CheckoutSessionData
|
|
|
24
|
+
|
|
|
25
|
+ class MyBasketMiddleware(BasketMiddleware):
|
|
|
26
|
+
|
|
|
27
|
+ def get_basket(self, request):
|
|
|
28
|
+ basket = super(MyBasketMiddleware, self).get_basket(request)
|
|
|
29
|
+ if self.is_tax_exempt(request):
|
|
|
30
|
+ basket.set_as_tax_exempt()
|
|
|
31
|
+ return basket
|
|
|
32
|
+
|
|
|
33
|
+ def is_tax_exempt(self, request):
|
|
|
34
|
+ country = self.get_shipping_address_country(request)
|
|
|
35
|
+ if country is None:
|
|
|
36
|
+ return False
|
|
|
37
|
+ return country.iso_3166_1_a2 not in ('GB',)
|
|
|
38
|
+
|
|
|
39
|
+ def get_shipping_address_country(self, request):
|
|
|
40
|
+ session = CheckoutSessionData(request)
|
|
|
41
|
+ if not session.is_shipping_address_set():
|
|
|
42
|
+ return None
|
|
|
43
|
+ addr_id = session.shipping_user_address_id()
|
|
|
44
|
+ if addr_id:
|
|
|
45
|
+ # User shipping to address from address book
|
|
|
46
|
+ return UserAddress.objects.get(id=addr_id).country
|
|
|
47
|
+ else:
|
|
|
48
|
+ fields = session.new_shipping_address_fields()
|
|
|
49
|
+
|
|
|
50
|
+Here we are using the checkout session wrapper to check if the user has set a
|
|
|
51
|
+shipping address. If they have, we extract the country and check its ISO
|
|
|
52
|
+3166 code.
|
|
|
53
|
+
|
|
|
54
|
+It is straightforward to extend this idea to apply custom tax exemptions to the
|
|
|
55
|
+basket based of different criteria.
|