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

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