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.5KB

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