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.

how_to_configure_shipping.rst 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. =========================
  2. How to configure shipping
  3. =========================
  4. Shipping can be very complicated. Depending on the domain, a wide variety of
  5. shipping scenarios are found in the wild. For instance, calculation of
  6. shipping costs can depend on:
  7. * Shipping method (e.g., standard, courier)
  8. * Shipping address
  9. * Time of day of order (e.g., if requesting next-day delivery)
  10. * Weight of items in basket
  11. * Customer type (e.g., business accounts get discounted shipping rates)
  12. * Offers and vouchers that give free or discounted shipping
  13. Further complications can arise such as:
  14. * Only making certain shipping methods available to certain customers
  15. * Tax is only applicable in certain situations
  16. Oscar can handle all of these shipping scenarios.
  17. Shipping in Oscar
  18. ~~~~~~~~~~~~~~~~~
  19. Configuring shipping charges requires overriding Oscar's core 'shipping' app
  20. and providing your own ``Repository`` class (see :doc:`/topics/customisation`) that
  21. returns your chosen shipping method instances.
  22. The primary responsibility of the
  23. ``Repository`` class is to provide the available shipping methods for a
  24. particular scenario. This is done via the
  25. :func:`~oscar.apps.shipping.repository.Repository.get_shipping_methods` method,
  26. which returns the shipping methods available to the customer.
  27. This method is called in several places:
  28. * To look up a "default" shipping method so that sample shipping charges can be
  29. shown on the basket detail page.
  30. * To list the available shipping methods on the checkout shipping method page.
  31. * To check the selected shipping method is still available when an order is
  32. submitted.
  33. The ``get_shipping_methods`` method takes the basket, user, shipping address
  34. and request as parameters. These can be used to provide different sets of
  35. shipping methods depending on the circumstances. For instance, you could use
  36. the shipping address to provide international shipping rates if the address is
  37. overseas.
  38. The ``get_default_shipping_method`` method takes the same parameters and
  39. returns default shipping method for the current basket. Used for shipping
  40. cost indication on the basket page. Defaults to free shipping method.
  41. .. note::
  42. Oscar's checkout process includes a page for choosing your shipping method.
  43. If there is only one method available for your basket (as is the default)
  44. then it will be chosen automatically and the user immediately redirected to
  45. the next step.
  46. Custom repositories
  47. -------------------
  48. If the available shipping methods are the same for all customers and shipping
  49. addresses, then override the ``methods`` property of the repository:
  50. .. code-block:: python
  51. from oscar.apps.shipping import repository
  52. from . import methods
  53. class Repository(repository.Repository):
  54. methods = (methods.Standard(), methods.Express())
  55. For more complex logic, override the ``get_available_shipping_methods`` method:
  56. .. code-block:: python
  57. from oscar.apps.shipping import repository
  58. from . import methods
  59. class Repository(repository.Repository):
  60. def get_available_shipping_methods(
  61. self, basket, user=None, shipping_addr=None,
  62. request=None, **kwargs):
  63. methods = (methods.Standard())
  64. if shipping_addr and shipping_addr.country.code == 'GB':
  65. # Express is only available in the UK
  66. methods = (methods.Standard(), methods.Express())
  67. return methods
  68. Note that the ``get_shipping_methods`` method wraps
  69. ``get_available_shipping_methods`` in order to handle baskets that don't
  70. require shipping and to apply shipping discounts.
  71. Shipping methods
  72. ----------------
  73. Shipping methods need to implement a certain API. They need to have the
  74. following properties which define the metadata about the shipping method:
  75. * ``code`` - This is used as an identifier for the shipping method and so should
  76. be unique amongst the shipping methods available in your shop.
  77. * ``name`` - The name of the shipping method. This will be visible to the
  78. customer during checkout.
  79. * ``description`` - An optional description of the shipping method. This can
  80. contain HTML.
  81. Further, each method must implement a ``calculate`` method which accepts the
  82. basket instance as a parameter and returns a ``Price`` instance. Most shipping
  83. methods subclass
  84. :class:`~oscar.apps.shipping.methods.Base`, which stubs this API.
  85. Here's an example:
  86. .. code-block:: python
  87. from oscar.apps.shipping import methods
  88. from oscar.core import prices
  89. class Standard(methods.Base):
  90. code = 'standard'
  91. name = 'Standard shipping (free)'
  92. def calculate(self, basket):
  93. return prices.Price(
  94. currency=basket.currency,
  95. excl_tax=D('0.00'), incl_tax=D('0.00'))
  96. Core shipping methods
  97. ~~~~~~~~~~~~~~~~~~~~~
  98. Oscar ships with several re-usable shipping methods which can be used as-is, or
  99. subclassed and customised:
  100. * :class:`~oscar.apps.shipping.methods.Free` - no shipping charges
  101. * :class:`~oscar.apps.shipping.methods.FixedPrice` - fixed-price shipping charges.
  102. Example usage:
  103. .. code-block:: python
  104. from oscar.apps.shipping import methods
  105. from oscar.core import prices
  106. class Standard(methods.FixedPrice):
  107. code = 'standard'
  108. name = 'Standard shipping'
  109. charge_excl_tax = D('5.00')
  110. class Express(methods.FixedPrice):
  111. code = 'express'
  112. name = 'Express shipping'
  113. charge_excl_tax = D('10.00')
  114. There is also a weight-based shipping method,
  115. :class:`~oscar.apps.shipping.abstract_models.AbstractWeightBased`
  116. which determines a shipping charge by calculating the weight of a basket's
  117. contents and looking this up in a model-based set of weight bands.