Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Send fulfillment confirmation email to user
Anna Siwik572bd5b19563
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.pyindex 6cc497bf02..00345fc4c9 100644--- a/saleor/core/utils/random_data.py+++ b/saleor/core/utils/random_data.py@@ -17,8 +17,8 @@ from ...account.models import Address, Userfrom ...account.utils import store_user_addressfrom ...discount import DiscountValueType, VoucherTypefrom ...discount.models import Sale, Voucher-from ...order import OrderStatusfrom ...order.models import Fulfillment, Order, Payment+from ...order.utils import update_order_statusfrom ...product.models import (AttributeChoiceValue, Category, Collection, Product, ProductAttribute,ProductImage, ProductType, ProductVariant, Stock, StockLocation)@@ -409,20 +409,13 @@ def create_order_lines(order, how_many=10):def create_fulfillments(order):- is_fulfilled = Truefor line in order:if random.choice([False, True]):fulfillment, _ = Fulfillment.objects.get_or_create(order=order)quantity = random.randrange(0, line.quantity) + 1fulfillment.lines.create(order_line=line, quantity=quantity)- is_fulfilled = is_fulfilled and line.is_fulfilled()-- if is_fulfilled:- order.status = OrderStatus.FULFILLED- order.save()- elif order.fulfillments.exists():- order.status = OrderStatus.PARTIALLY_FULFILLED- order.save()++ update_order_status(order)def create_fake_order():diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.pyindex 74453d6b30..08185c8edf 100644--- a/saleor/dashboard/order/forms.py+++ b/saleor/dashboard/order/forms.py@@ -366,10 +366,18 @@ class AddressForm(StorefrontAddressForm):class FulfillmentForm(forms.ModelForm):"""Create fulfillment group for a given order."""+ send_mail = forms.BooleanField(+ initial=True, required=False, label=pgettext_lazy(+ 'Send mail to customer',+ 'Send shipment details to your customer now'))class Meta:model = Fulfillmentfields = ['tracking_number']+ labels = {+ 'tracking_number': pgettext_lazy(+ 'Order tracking number',+ 'Tracking number')}def __init__(self, *args, **kwargs):order = kwargs.pop('order')diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.pyindex 4d85095658..b78d2b4973 100644--- a/saleor/dashboard/order/views.py+++ b/saleor/dashboard/order/views.py@@ -15,8 +15,10 @@ from prices import Money, TaxedMoneyfrom ...core.exceptions import InsufficientStockfrom ...core.utils import ZERO_TAXED_MONEY, get_paginator_itemsfrom ...order import OrderStatus+from ...order.emails import send_fulfillment_confirmationfrom ...order.models import (FulfillmentLine, Fulfillment, Order, OrderLine, OrderNote)+from ...order.utils import update_order_statusfrom ...product.models import StockLocationfrom ..views import staff_member_requiredfrom .filters import OrderFilter@@ -412,10 +414,7 @@ def fulfill_order_lines(request, order_pk):line.fulfillment = fulfillmentline.save()quantity_fulfilled += line_form.cleaned_data.get('quantity')- order.status = (- OrderStatus.FULFILLED if order.is_fulfilled()- else OrderStatus.PARTIALLY_FULFILLED)- order.save(update_fields=['status'])+ update_order_status(order)msg = npgettext_lazy('Dashboard message related to an order','Fulfilled %(quantity_fulfilled)d item',@@ -423,6 +422,13 @@ def fulfill_order_lines(request, order_pk):'quantity_fulfilled') % {'quantity_fulfilled': quantity_fulfilled}order.history.create(content=msg, user=request.user)+ if form.cleaned_data.get('send_mail'):+ send_fulfillment_confirmation.delay(order.pk, fulfillment.pk)+ send_mail_msg = pgettext_lazy(+ 'Dashboard message related to an order',+ 'Shipping confirmation email was sent to user'+ '(%(email)s)') % {'email': order.get_user_current_email()}+ order.history.create(content=send_mail_msg, user=request.user)else:msg = pgettext_lazy('Dashboard message related to an order', 'No items fulfilled')diff --git a/saleor/order/emails.py b/saleor/order/emails.pyindex bc6bf06139..15bca34047 100644--- a/saleor/order/emails.py+++ b/saleor/order/emails.py@@ -5,9 +5,10 @@ from django.urls import reversefrom templated_email import send_templated_mailfrom ..core.utils import build_absolute_uri-from .models import Order+from .models import Fulfillment, OrderCONFIRM_ORDER_TEMPLATE = 'source/order/confirm_order'+CONFIRM_FULFILLMENT_TEMPLATE = 'source/order/confirm_fulfillment'CONFIRM_PAYMENT_TEMPLATE = 'source/order/payment/confirm_payment'CONFIRM_NOTE_TEMPLATE = 'source/order/note/confirm_note'@@ -44,6 +45,14 @@ def send_order_confirmation(order_pk):_send_confirmation(**email_data)+@shared_task+def send_fulfillment_confirmation(order_pk, fulfillment_pk):+ email_data = collect_data_for_email(order_pk, CONFIRM_FULFILLMENT_TEMPLATE)+ fulfillment = Fulfillment.objects.get(pk=fulfillment_pk)+ email_data.update({'context': {'fulfillment': fulfillment}})+ _send_confirmation(**email_data)++@shared_taskdef send_payment_confirmation(order_pk):email_data = collect_data_for_email(order_pk, CONFIRM_PAYMENT_TEMPLATE)diff --git a/saleor/order/models.py b/saleor/order/models.pyindex 90d45da9ff..9ac51e510e 100644--- a/saleor/order/models.py+++ b/saleor/order/models.py@@ -127,12 +127,9 @@ class Order(models.Model):def is_pre_authorized(self):return self.payments.filter(status=PaymentStatus.PREAUTH).exists()- def is_unfulfilled(self):- return not self.fulfillments.filter(- status=FulfillmentStatus.FULFILLED).exists()-- def is_fulfilled(self):- return all([line.is_fulfilled() for line in self])+ @property+ def quantity_fulfilled(self):+ return sum([line.quantity_fulfilled for line in self])def is_shipping_required(self):return any(line.is_shipping_required for line in self)diff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex 714edd26ce..94f6fe08aa 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -57,6 +57,23 @@ def cancel_order(order, restock):order.save(update_fields=['status'])+def update_order_status(order):+ """Update order status depending on fulfillments."""+ quantity_fulfilled = order.quantity_fulfilled+ total_quantity = order.get_total_quantity()++ if quantity_fulfilled <= 0:+ status = OrderStatus.UNFULFILLED+ elif quantity_fulfilled < total_quantity:+ status = OrderStatus.PARTIALLY_FULFILLED+ else:+ status = OrderStatus.FULFILLED++ if status != order.status:+ order.status = status+ order.save(update_fields=['status'])++def cancel_fulfillment(fulfillment, restock):"""Cancel fulfillment.@@ -65,10 +82,7 @@ def cancel_fulfillment(fulfillment, restock):restock_fulfillment_lines(fulfillment)fulfillment.status = FulfillmentStatus.CANCELEDfulfillment.save(update_fields=['status'])- order = fulfillment.order- if order.is_unfulfilled():- order.status = OrderStatus.UNFULFILLED- order.save(update_fields=['status'])+ update_order_status(fulfillment.order)def attach_order_to_user(order, user):diff --git a/templates/dashboard/order/fulfillment.html b/templates/dashboard/order/fulfillment.htmlindex c7fc155912..8fe19c7a2f 100644--- a/templates/dashboard/order/fulfillment.html+++ b/templates/dashboard/order/fulfillment.html@@ -113,7 +113,8 @@</table></div><div class="col s12 m6">- {{ form }}+ {{ form.tracking_number|materializecss }}+ {{ form.send_mail|materializecss }}</div></div></div>diff --git a/templates/templated_email/source/order/confirm_fulfillment.email b/templates/templated_email/source/order/confirm_fulfillment.emailnew file mode 100644index 0000000000..d52df5c6e1--- /dev/null+++ b/templates/templated_email/source/order/confirm_fulfillment.email@@ -0,0 +1,36 @@+{% load i18n %}+{% load gross from prices_i18n %}++{% block subject %}+ {% blocktrans trimmed with order=fulfillment.order context "Fulfillment confirmation email subject" %}+ A shipment from order {{ order }} is on the way+ {% endblocktrans %}+{% endblock %}++{% block plain %}+{% blocktrans context "Fulfillment confirmation email text" %}+Hi!++Thank you for your order. Below is the list of shipped products.+{% endblocktrans %}+{% if fulfillment.tracking_number %}+{% blocktrans trimmed with tracking_number=fulfillment.tracking_number context "Fulfillment confirmation email text" %}+You can track your shipment with {{ tracking_number }} code.+{% endblocktrans %}+{% endif %}++{% for line in fulfillment %}+{{ line.quantity }} x {{ line.order_line.product_name }}+{% endfor %}++{% blocktrans context "Fulfillment confirmation email text" %}+This is an automatically generated email, please do not reply.++Sincerely,+{{ site_name }}+{% endblocktrans %}+{% endblock %}++{% block html %}+{% include 'templated_email/compiled/confirm_fulfillment.html' %}+{% endblock %}diff --git a/templates/templated_email/source/order/confirm_fulfillment.mjml b/templates/templated_email/source/order/confirm_fulfillment.mjmlnew file mode 100644index 0000000000..4afe3a530d--- /dev/null+++ b/templates/templated_email/source/order/confirm_fulfillment.mjml@@ -0,0 +1,36 @@+<mjml>+ <mj-head>+ <mj-include path="../shared/styles" />+ </mj-head>+ <mj-body>+ <mj-container>+ <mj-section css-class="no-display">+ <mj-column>+ <mj-text>+ {% load i18n %}+ </mj-text>+ </mj-column>+ </mj-section>+ <mj-include path="../shared/header" />+ <mj-section>+ <mj-column>+ <mj-text font-size="16px">+ {% trans "Hi!" context "Fulfillment confirmation email text" %}+ </mj-text>+ <mj-text>+ {% blocktrans trimmed context "Fulfillment confirmation email text" %}+ Thank you for your order. Below is the list of shipped products.+ {% endblocktrans %}+ {% if fulfillment.tracking_number %}+ {% blocktrans trimmed with tracking_number=fulfillment.tracking_number context "Fulfillment confirmation email text" %}+ You can track your shipment with {{ tracking_number }} code.+ {% endblocktrans %}+ {% endif %}+ </mj-text>+ </mj-column>+ </mj-section>+ <mj-include path="./partials/_fulfillment_lines" />+ <mj-include path="../shared/footer" />+ </mj-container>+ </mj-body>+</mjml>diff --git a/templates/templated_email/source/order/partials/_fulfillment_lines.mjml b/templates/templated_email/source/order/partials/_fulfillment_lines.mjmlnew file mode 100644index 0000000000..279df47c2b--- /dev/null+++ b/templates/templated_email/source/order/partials/_fulfillment_lines.mjml@@ -0,0 +1,27 @@+<mj-section css-class="no-display">+ <mj-column>+ <mj-text>+ {% load gross from prices_i18n %}+ </mj-text>+ </mj-column>+</mj-section>+<mj-section>+ <mj-column>+ <mj-table>+ <thead class="table-header-row">+ <tr>+ <th>Item</th>+ <th>Quantity</th>+ </tr>+ </thead>+ <tbody>+ {% for line in fulfillment %}+ <tr class="table-item-row">+ <td>{{ line.order_line.product_name }}</td>+ <td style="text-align: right;">{{ line.quantity }}</td>+ </tr>+ {% endfor %}+ </tbody>+ </mj-table>+ </mj-column>+</mj-section>diff --git a/templates/templated_email/source/shared/footer.mjml b/templates/templated_email/source/shared/footer.mjmlindex ca620c9a4c..5baf40405d 100644--- a/templates/templated_email/source/shared/footer.mjml+++ b/templates/templated_email/source/shared/footer.mjml@@ -1,8 +1,8 @@<mj-section><mj-column><mj-text>- {% blocktrans trimmed context "Base e-mail text" %}- This is an automatically generated e-mail, please do not reply.+ {% blocktrans trimmed context "Base email text" %}+ This is an automatically generated email, please do not reply.{% endblocktrans %}</mj-text></mj-column>@@ -10,7 +10,7 @@<mj-section background-color="#F2F2F2"><mj-column><mj-text align="center">- {% blocktrans trimmed context "Base e-mail footer" %}+ {% blocktrans trimmed context "Base email footer" %}Sincerely, {{ site_name }}{% endblocktrans %}</mj-text>