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.

prices_and_availability.rst 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. =======================
  2. Prices and availability
  3. =======================
  4. This page explains how prices and availability are determined in Oscar. In
  5. short, it seems quite complicated as there are several parts to it, but what
  6. this buys is flexility: buckets of it.
  7. Overview
  8. --------
  9. Simpler e-commerce frameworks often tie prices to the product model directly:
  10. .. code-block:: python
  11. product = Product.objects.get(id=1)
  12. print "Price is %.2f" % product.price_incl_tax
  13. Oscar, on the other hand, distinguishes products from stockrecords and provides
  14. a swappable 'strategy' component for selecting the appropriate stockrecord,
  15. calculating prices and availability information.
  16. .. code-block:: python
  17. product = Product.objects.get(id=1)
  18. stockinfo = strategy.fetch(product)
  19. print "Price is %.2f" % stockinfo.price.incl_tax
  20. The product model captures the core data about the product (title, description,
  21. images) while a stockrecord represents fulfillment information for one
  22. particular partner (number in stock, base price). A product can have multiple
  23. stockrecords although only one is selected by the strategy to determine pricing and
  24. availability.
  25. By using your own custom strategy class, a wide range of pricing, tax and
  26. availability problems can be easily solved.
  27. .. note::
  28. Oscar's handling of prices and availability was reworked for v0.6.
  29. The old APIs are still available but considered deprecated and
  30. will be removed in Oscar 0.7.
  31. .. _strategy_class:
  32. The strategy class
  33. ------------------
  34. Oscar uses a 'strategy' object to determine product availability and pricing. A
  35. new strategy instance is assigned to the request by the basket middleware. A
  36. overridable ``Selector`` class determines the appropriate strategy for the
  37. request.
  38. Given a product, the strategy class is responsible for:
  39. - Selecting a "pricing policy", an object detailing the prices of the product and whether tax is known.
  40. - Selecting an "availability policy", an object responsible for
  41. availability logic (ie is the product available to buy) and customer
  42. messaging.
  43. - Selecting the appropriate stockrecord to use for fulfillment
  44. These three entities are wrapped up in a ``StockInfo`` object, which is a simple
  45. named tuple. The strategy class provides a ``fetch`` method which takes a
  46. product and returns a ``StockInfo`` instance:
  47. .. code-block:: python
  48. # ... in view code ...
  49. stock_info = request.strategy.fetch(product)
  50. if stock_info.availability.is_available_to_buy:
  51. print "Price incl tax is %s" % stock_info.price.incl_tax
  52. print "Selected stockrecord is %s" % stock_info.stockrecord
  53. The strategy class is accessed in several places in Oscar's codebase. In templates, a
  54. ``session_strategy`` template tag is used to load the price and availabilty
  55. information into the template context:
  56. .. code-block:: html+django
  57. {% load stockrecord_tags %}
  58. {% load currency_filters %}
  59. {% session_strategy request product as session %}
  60. <p>
  61. {% if session.price.is_tax_known %}
  62. Price is {{ session.price.incl_tax|currency:session.price.currency }}
  63. {% else %}
  64. Price is {{ session.price.excl_tax|currency:session.price.currency }} +
  65. tax
  66. {% endif %}
  67. </p>
  68. Note that the ``currency`` template tag accepts a currency parameter from the
  69. pricing policy.
  70. Also, basket instances have a strategy instance assigned so they can calculate
  71. prices including taxes. This is done automatically in the basket middleware.
  72. This seems quite complicated...
  73. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  74. While this probably seems like quite an involved way of looking up a product's
  75. price, it gives the developer an immense amount of flexibility. Here's a few
  76. examples of things you can do with a strategy class:
  77. - Transact in multiple currencies. The strategy
  78. class can use the customer's location to select a stockrecord from a local
  79. distribution partner and to set the appropriate currency for the price.
  80. - Elegantly handle different tax models. A strategy can return prices including
  81. tax for a UK or European visitor, but without tax for US
  82. visitors where tax is only determined once shipping details are confirmed.
  83. - Charge different prices to different customers. A strategy can return a
  84. different pricing policy depending on the user/session.
  85. - Use a chain of preferred partners for fulfillment. A site could have many
  86. stockrecords for the same product, each from a different fulfillment partner.
  87. The strategy class could select the partner with the best margin and stock
  88. available. When stock runs out with that partner, the strategy could
  89. seamlessly switch to the next best partner.
  90. These are the kinds of problems that other e-commerce frameworks would struggle
  91. with.
  92. API
  93. ~~~
  94. A strategy class need only implement one method, as defined by the ``Base``
  95. class:
  96. .. autoclass:: oscar.apps.partner.strategy.Base
  97. :members:
  98. :noindex:
  99. Oscar also provides a "structured" strategy class which provides overridable
  100. methods for selecting the stockreord, and determining pricing and availabiltiy
  101. policies:
  102. .. autoclass:: oscar.apps.partner.strategy.Structured
  103. :members:
  104. :noindex:
  105. For most projects, subclassing and overriding the ``Structured`` base class
  106. should be sufficient. However, Oscar also provides mixins to easily compose the
  107. appropriate strategy class for your domain.
  108. Loading a strategy
  109. ------------------
  110. Strategy instances are determined by the ``Selector`` class:
  111. .. autoclass:: oscar.apps.partner.strategy.Selector
  112. :members:
  113. :noindex:
  114. It's common to override this class so a custom strategy class can be returned.
  115. .. _pricing_policies:
  116. Pricing policies
  117. ----------------
  118. A pricing policy is a simple class with several properties Its job is to
  119. contain all price and tax information about a product.
  120. There is a base class that defines the interface a pricing policy should have:
  121. .. autoclass:: oscar.apps.partner.prices.Base
  122. :members:
  123. :noindex:
  124. There are also several policies that accommodate common scenarios:
  125. .. automodule:: oscar.apps.partner.prices
  126. :members: Unavailable, FixedPrice, DelegateToStockRecord
  127. :noindex:
  128. .. _availability_policies:
  129. Availability policies
  130. ---------------------
  131. Like pricing policies, availability policies are simple classes with several
  132. properties and methods. The job of an availability policy is to provide
  133. availability messaging to show to the customer as well as methods to determine
  134. if the product is available to buy.
  135. The base class defines the interface:
  136. .. autoclass:: oscar.apps.partner.availability.Base
  137. :members:
  138. :noindex:
  139. There are also several pre-defined availability policies:
  140. .. automodule:: oscar.apps.partner.availability
  141. :members: Unavailable, Available, StockRequired, DelegateToStockRecord
  142. :noindex:
  143. Strategy mixins
  144. ---------------
  145. Oscar also ships with several mixins which implement one method of the
  146. ``Structured`` stategy. These allow strategies to be easily
  147. composed from re-usable parts:
  148. .. automodule:: oscar.apps.partner.strategy
  149. :members: UseFirstStockRecord, StockRequired, NoTax, FixedRateTax,
  150. DeferredTax
  151. :noindex:
  152. Default strategy
  153. ----------------
  154. Oscar's default ``Selector`` class returns a ``Default`` strategy built from
  155. the strategy mixins:
  156. .. code-block:: python
  157. class Default(UseFirstStockRecord, StockRequired, NoTax, Structured):
  158. pass
  159. The behaviour of this strategy is:
  160. - Always picks the first stockrecord (this is backwards compatible with
  161. Oscar<0.6 where a product could only have one stockrecord).
  162. - Charge no tax.
  163. - Only allow purchases where there is appropriate stock (eg no back-orders).
  164. How to use
  165. ----------
  166. There's lots of ways to use strategies, pricing and availability policies to
  167. handle your domain's requirements.
  168. The normal first step is provide your own ``Selector`` class which returns a custom
  169. strategy class. Your custom strategy class can be composed of the above mixins
  170. or your own custom logic.
  171. Example 1: UK VAT
  172. ~~~~~~~~~~~~~~~~~
  173. Here's an example ``strategy.py`` module which is used to charge VAT on prices.
  174. .. code-block:: python
  175. # myproject/partner/strategy.py
  176. from oscar.apps.partner import strategy, prices
  177. class Selector(object):
  178. """
  179. Custom selector to return a UK-specific strategy that charges VAT
  180. """
  181. def strategy(self, request=None, user=None, **kwargs):
  182. return UKStrategy(territory)
  183. class IncludingVAT(strategy.FixedRateTax):
  184. """
  185. Price policy to charge VAT on the base price
  186. """
  187. # We can simply override the tax rate on the core FixedRateTax. Note
  188. # this is a simplification: in reality, you might want to store tax
  189. # rates and the date ranges they apply in a database table. Your
  190. # pricing policy could simply look up the appropriate rate.
  191. rate = D('0.20')
  192. class UKStrategy(strategy.UseFirstStockRecord, IncludingVAT,
  193. strategy.StockRequired, strategy.Structured):
  194. """
  195. Typical UK strategy for physical goods.
  196. - There's only one warehouse/partner so we use the first and only stockrecord
  197. - Enforce stock level. Don't allow purchases when we don't have stock.
  198. - Charge UK VAT on prices. Assume everything is standard-rated.
  199. """
  200. Example 2: US sales tax
  201. ~~~~~~~~~~~~~~~~~~~~~~~
  202. Here's an example ``strategy.py`` module which is suitable for use in the US
  203. where taxes can't be calculated until the shipping address is known. You
  204. normally need to use a 3rd party service to determine taxes - details omitted
  205. here.
  206. .. code-block:: python
  207. from oscar.apps.partner import strategy, prices
  208. class Selector(object):
  209. """
  210. Custom selector class to returns a US strategy
  211. """
  212. def strategy(self, request=None, user=None, **kwargs):
  213. return USStrategy()
  214. class USStrategy(strategy.UseFirstStockRecord, strategy.DeferredTax,
  215. strategy.StockRequired, strategy.Structured):
  216. """
  217. Typical US strategy for physical goods. Note we use the ``DeferredTax``
  218. mixin to ensure prices are returned without tax.
  219. - Use first stockrecord
  220. - Enforce stock level
  221. - Taxes aren't known for prices at this stage
  222. """