Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

how_to_apply_tax_exemptions.rst 1.9KB

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