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_customise_oscar_communications.rst 3.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. Customising Oscar's communications
  2. ==================================
  3. Oscar provides the ability to customise the emails sent out to customers.
  4. There are two main ways this can be achieved, either in code (via template
  5. files) or in the database (via Dashboard > Content > Email templates).
  6. Communications API
  7. ------------------
  8. First, it's important to understand a little about how the Communications API
  9. works.
  10. Oscar has a model called a ``CommunicationEventType``. When preparing an email
  11. to send out to a customer, the client code will do something like this::
  12. commtype_code = 'SOME_EVENT'
  13. context = {'customer': customer, 'something_else': 'Some more context.'}
  14. try:
  15. event_type = CommunicationEventType.objects.get(code=commtype_code)
  16. except CommunicationEventType.DoesNotExist:
  17. messages = CommunicationEventType.objects.get_and_render(commtype_code, ctx)
  18. else:
  19. messages = event_type.get_messages(ctx)
  20. What's happening here is:
  21. - The code defines an arbitrary communication type code to be treated as the
  22. reference for this particular type of communication. For example, the
  23. communication type code used when sending an order email confirmation is
  24. ``'ORDER_PLACED'``.
  25. - The database is checked for a ``CommunicationEventType`` with this
  26. communication type code. If it does, it renders the messages using that model
  27. instance, passing in some context.
  28. - Otherwise, it uses the ``get_and_render()`` method to render the messages,
  29. which uses templates instead.
  30. So, your first step when customising the emails sent out is to work out what
  31. communication type code is being used to send out the email. The easiest way to
  32. work this out is usually to look through the email templates in
  33. :file:`templates/oscar/communication/emails`: if the email template is called, say,
  34. :file:`commtype_order_placed_body.html`, then the code will be ``'ORDER_PLACED'``.
  35. See 'Customising through code' below.
  36. Customising through code
  37. ------------------------
  38. Customising emails through code uses Django's standard template inheritance.
  39. The first step is to locate the template for the particular email, which is
  40. usually in :file:`templates/oscar/communication/emails`. Then, in a template directory that
  41. takes precedence over the oscar templates directory, copy the file and customise
  42. it. For example, to override the
  43. :file:`templates/oscar/communication/emails/commtype_order_placed_body.html` template,
  44. create :file:`oscar/communication/emails/commtype_order_placed_body.html` in your
  45. template directory.
  46. Note that usually emails have three template files associated with them: the
  47. email subject line (:file:`commtype_{CODE}_subject.txt`), the html version
  48. (:file:`commtype_{CODE}_body.html`) and the text version (:file:`commtype_{CODE}_body.txt`).
  49. Usually you will want to make sure you override BOTH the html and the text
  50. version.
  51. Customising through code will not work if there is a template defined in the
  52. database instead (see below).
  53. Customising through the database
  54. --------------------------------
  55. Oscar provides a dashboard interface to allow admins to customise the emails.
  56. To enable this for a particular communication event type, log in to the admin
  57. site and create a new ``CommunicationEventType``. The code you use is the
  58. important thing: it needs to match the communication event code used when
  59. rendering the messages. For example, to override the order confirmation email,
  60. you need to create a ``CommunicationEventType`` with a code ``'ORDER_PLACED'``.
  61. Once you have created the ``CommunicationEventType``, you can edit it using the
  62. (much better) dashboard interface at Dashboard > Content > Email templates.
  63. If you have an email template defined in the database it will override any
  64. template files.