Django-Oscar allows admins to create templates for recurring emails.
Multiple necessities can show up.
For instance, when the store owner receives a new order, it would be helpful to be notified about it.
Using Django-Oscar’s email templates, this can be prepared in advance.
To start we need to create a communication event type.
Creating a COMMUNICATION EVENT TYPE
- Access the admin page and authenticate with your superuser
http://localhost:8000/admin/
- After authenticating go to:
COMMUNICATION → Communication event types
or access directly:
http://localhost:8000/admin/communication/communicationeventtype/
- Click ADD COMMUNICATION EVENT TYPE
- Choose a Code, Name and Category
- Example Code: NOTIFY_STORE
- Example Name: Notify store owner of a new order placement
- Example Category: Order Related
- Save and go to Dashboard
http://localhost:8000/dashboard/
Creating an email template
Django-Oscar has built in signals such as order_placed that allow us to run a piece of code everytime an order is placed.
This can be useful to notify the online store owner that a new order was placed and that action is then required for shipping.
- In the Dashboard go to:
Content → Email Templates
- Click the Edit button for the code NOTIFY_STORE
- Edit the following fields:
New Order Placed #{{ order.number }}New Order Placed #{{ order.number }}{% extends "oscar/communication/emails/base.html" %} {% load currency_filters i18n url_tags %} {% block tbody %} <tr> <td class="content-block"> <p xmlns="http://www.w3.org/1999/html">{% trans 'Hello,' %}</p> <p>{% blocktrans with order_number=order.number %} Hi jaimedcsilva, a new order {{ order_number }} has been placed. {% endblocktrans %} </p> </td> </tr> <tr> <td class="content-block"> <table class="order"> <tbody><tr> <td>{% trans 'Your order contains:' %}</td> </tr> <tr> <td> <table class="order-items" cellpadding="0" cellspacing="0"> <tbody> {% for line in order.lines.all %} <tr> <td>{{ line.title }} × {{ line.quantity }}</td> <td class="alignright">{{ line.line_price_incl_tax|currency:order.currency }}</td> </tr> {% endfor %} <tr> <td class="alignright">{% trans 'Basket total:' %}</td> <td class="alignright">{{ order.basket_total_incl_tax|currency:order.currency }}</td> </tr> <tr> <td class="alignright">{% trans 'Shipping:' %}</td> <td class="alignright">{{ order.shipping_incl_tax|currency:order.currency }}</td> </tr> <tr class="total"> <td class="alignright" width="80%">{% trans 'Order Total:' %}</td> <td class="alignright">{{ order.total_incl_tax|currency:order.currency }}</td> </tr> </tbody> </table> </td> </tr> </tbody></table> </td> </tr> <tr> <td class="content-block"> <p>{% trans 'Shipping address:' %}</p> <p>{% for field in order.shipping_address.active_address_fields %} {{ field }}<br/> {% endfor %}</p> </td> </tr> {% if status_url %} <tr> <td class="content-block"> {% absolute_url site.domain status_path as absolute_status_url %} {% blocktrans %}You can view the status of this order by clicking <a href="{{ absolute_status_url }}" title="order status">here</a> {% endblocktrans %} </td> </tr> {% endif %} {% endblock %}
We can use the email sent to the clients upon an order placement in:
mysite/templates/oscar/communication/emails/commtype_order_placed_body.html
and adapt it to the context of notifying the store owner.
Notifying the Store owner
Django-Oscar has built-in signals, such as order_placed, that allow us to run custom code every time an order is placed.
This can be useful to notify the online store owner that a new order was placed and that an action is required for shipping.
from django.dispatch import receiver
from oscar.apps.order.signals import order_placed
from oscar.apps.communication.models import *
from oscar.apps.communication.utils import *
@receiver(order_placed)
def notify_store(order, user, **kwargs):
event_code = 'NOTIFY_STORE'
email = "jaimedcsilva@hotmail.com"
ctx = {
"user": user,
"order": order,
"lines": order.lines.all(),
"site": {
"name": "jaimedcsilva.com"
}
}
msgs = CommunicationEventType.objects.get_and_render(event_code, ctx)
dispatch = Dispatcher()
dispatch.send_email_messages(email, msgs)
Tested with: Django 5.2 Django-Oscar 4
11 July 2024
|
Last Updated: 02 Dec. 2025
|
jaimedcsilva Related