|
@@ -176,15 +176,16 @@ Shipping
|
176
|
176
|
The shipping method API has been altered to avoid potential thread-safety
|
177
|
177
|
issues. Prior to v0.8, shipping methods had a ``set_basket`` method which
|
178
|
178
|
allowed a basket instance to be assigned. This was really a crutch to allow
|
179
|
|
-templates to have easy access to shipping charges. However, it was also a
|
|
179
|
+templates to have easy access to shipping charges (as they could be read
|
|
180
|
+straight off the shipping method instance). However, it was also a
|
180
|
181
|
design problem as shipping methods could be instantiated at compile-time
|
181
|
182
|
leading to a thread safety issue where multiple threads could assign a basket
|
182
|
183
|
to the same shipping method instance.
|
183
|
184
|
|
184
|
185
|
In Oscar 0.8, shipping methods are stateless services that have a method
|
185
|
186
|
:func:`~oscar.apps.shipping.methods.Base.calculate` that takes a basket and
|
186
|
|
-returns a ``Price`` instance. New template tags are provided that allow these
|
187
|
|
-shipping charges to be accessed from templates.
|
|
187
|
+returns a ``Price`` instance. New :doc:`template tags </ref/templatetags/>` are
|
|
188
|
+provided that allow these shipping charges to be accessed from templates.
|
188
|
189
|
|
189
|
190
|
This API change does require quite a few changes as both the shipping method
|
190
|
191
|
and shipping charge now need to be passed around separately:
|
|
@@ -204,6 +205,49 @@ and shipping charge now need to be passed around separately:
|
204
|
205
|
method has changed to accept the ``shipping_charge`` rather than a
|
205
|
206
|
``shipping_method`` instance.
|
206
|
207
|
|
|
208
|
+Another key change is in the shipping repository object. The
|
|
209
|
+``get_shipping_methods`` method has been split in two to simplify the exercise
|
|
210
|
+of providing new shipping methods. The best practice for Oscar 0.8 is to
|
|
211
|
+override the ``methods`` attribute if the same set of shipping methods is
|
|
212
|
+available to everyone:
|
|
213
|
+
|
|
214
|
+.. code-block:: python
|
|
215
|
+
|
|
216
|
+ from oscar.apps.shipping import repository, methods
|
|
217
|
+
|
|
218
|
+ class Standard(methods.FixedPrice):
|
|
219
|
+ code = "standard"
|
|
220
|
+ name = "Standard"
|
|
221
|
+ charge_excl_tax = D('10.00')
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+ class Express(methods.FixedPrice):
|
|
225
|
+ code = "express"
|
|
226
|
+ name = "Express"
|
|
227
|
+ charge_excl_tax = D('20.00')
|
|
228
|
+
|
|
229
|
+ class Repository(repository.Repository):
|
|
230
|
+ methods = [Standard(), Express()]
|
|
231
|
+
|
|
232
|
+or to override ``get_available_shipping_methods`` if the available shipping
|
|
233
|
+methods if only available conditionally:
|
|
234
|
+
|
|
235
|
+.. code-block:: python
|
|
236
|
+
|
|
237
|
+ from oscar.apps.shipping import repository
|
|
238
|
+
|
|
239
|
+ class Repository(repository.Repository):
|
|
240
|
+
|
|
241
|
+ def get_available_shipping_methods(
|
|
242
|
+ self, basket, shipping_addr=None, **kwargs):
|
|
243
|
+ methods = [Standard()]
|
|
244
|
+ if shipping_addr.country.code == 'US':
|
|
245
|
+ # Express only available in the US
|
|
246
|
+ methods.append(Express())
|
|
247
|
+ return methods
|
|
248
|
+
|
|
249
|
+Note that shipping address should be passed around as instances not classes.
|
|
250
|
+
|
207
|
251
|
Other potentially breaking changes related to shipping include:
|
208
|
252
|
|
209
|
253
|
* The :class:`~oscar.apps.order.utils.OrderCreator` class no longer defaults to
|