You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

custom_shipping_logic.rst 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. ===========================
  2. How to set shipping charges
  3. ===========================
  4. By default, you can configure shipping by using the built-in ShippingMethod models. These
  5. support shipping charges that are calculated using an order- and item-level charge.
  6. Custom shipping calculators
  7. ---------------------------
  8. To use a custom shipping calculator, you need to subclass the core shipping Repository class and
  9. override two methods in provide the calculator of your domain.
  10. First create a ``myshop.shipping`` app and include it in your ``settings.py`` file (removing the ``oscar.shipping``
  11. app in the process.
  12. Next, create ``methods.py`` and create a new ``Repository`` class that subclasses the core ``Repository`` class but
  13. provides the custom behaviour that you need.
  14. Here is an example ``methods.py``::
  15. from decimal import Decimal
  16. from oscar.shipping.methods import Repository as CoreRepository
  17. from oscar.shipping.abstract_models import ShippingMethod
  18. class FixedChargeMethod(ShippingMethod):
  19. name = 'Fixed charge'
  20. def basket_charge_incl_tax(self):
  21. return Decimal('12.50')
  22. def basket_charge_excl_tax(self):
  23. return Decimal('12.50')
  24. class Repository(CoreRepository):
  25. def __init__(self):
  26. self.method = FixedChargeMethod()
  27. def get_shipping_methods(self, user, basket):
  28. return [self.method]
  29. def find_by_code(self, code):
  30. return self.method
  31. Here we are using a plain Python object (not a Django model) as the shipping calculator.