Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Send fulfillment email when automatic fulfillment is enabled

Maciej Korycinski114a12a521fe
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index 371c98703b..f88eaf3158 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -12,7 +12,7 @@ from ..core.utils.taxes import (
from ..core.weight import zero_weight
from ..dashboard.order.utils import get_voucher_discount_for_order
from ..discount.models import NotApplicable
-from ..order import FulfillmentStatus, OrderStatus
+from ..order import FulfillmentStatus, OrderStatus, emails
from ..order.models import Fulfillment, FulfillmentLine, Order, OrderLine
from ..payment import ChargeStatus
from ..payment.utils import gateway_refund, gateway_void
@@ -57,14 +57,14 @@ def fulfill_order_line(order_line, quantity):
def automatically_fulfill_digital_lines(order: Order):
"""Fulfill all digital lines which have enabled automatic fulfillment
- setting"""
+ setting and send confirmation email."""
digital_lines = order.lines.filter(
is_shipping_required=False, variant__digital_content__isnull=False)
digital_lines = digital_lines.prefetch_related('variant__digital_content')
if not digital_lines:
return
- fulfilment, _ = Fulfillment.objects.get_or_create(order=order)
+ fulfillment, _ = Fulfillment.objects.get_or_create(order=order)
for line in digital_lines:
if not order_line_needs_automatic_fulfillment(line):
continue
@@ -72,10 +72,10 @@ def automatically_fulfill_digital_lines(order: Order):
digital_content.urls.create(line=line)
quantity = line.quantity
FulfillmentLine.objects.create(
- fulfillment=fulfilment, order_line=line,
+ fulfillment=fulfillment, order_line=line,
quantity=quantity)
fulfill_order_line(order_line=line, quantity=quantity)
-
+ emails.send_fulfillment_confirmation.delay(order.pk, fulfillment.pk)
def check_order_status(func):
"""Check if order meets preconditions of payment process.
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index e46d1db8ca..86cf94134e 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -12,10 +12,9 @@ from django.utils.translation import pgettext_lazy
from ..account.models import Address, User
from ..checkout.models import Cart
from ..core import analytics
-from ..order import OrderEvents, OrderEventsEmails
+from ..order import OrderEvents, OrderEventsEmails, utils as order_utils
from ..order.emails import send_payment_confirmation
from ..order.models import Order
-from ..order import utils as order_utils
from . import (
ChargeStatus, CustomPaymentChoices, GatewayError, OperationType,
PaymentError, TransactionKind, get_payment_gateway)
@@ -80,7 +79,6 @@ def handle_fully_paid_order(order):
if order_utils.order_needs_automatic_fullfilment(order):
order_utils.automatically_fulfill_digital_lines(order)
- # send_email
try:
analytics.report_order(order.tracking_client_id, order)
except Exception:
diff --git a/tests/test_order.py b/tests/test_order.py
index 56f8117732..6be379f769 100644
--- a/tests/test_order.py
+++ b/tests/test_order.py
@@ -1,5 +1,6 @@
from decimal import Decimal
from unittest.mock import patch
+
import pytest
from django.urls import reverse
from django_countries.fields import Country
@@ -12,16 +13,17 @@ from saleor.core.utils.taxes import (
DEFAULT_TAX_RATE_NAME, get_tax_rate_by_name, get_taxes_for_country)
from saleor.core.weight import zero_weight
from saleor.order import FulfillmentStatus, OrderStatus, models
-from saleor.order.models import Order, Fulfillment
+from saleor.order.models import Fulfillment, Order
from saleor.order.utils import (
- add_variant_to_order, cancel_fulfillment, cancel_order,
- change_order_line_quantity, delete_order_line, recalculate_order,
- fulfill_order_line, restock_fulfillment_lines, restock_order_lines,
- update_order_prices, update_order_status, automatically_fulfill_digital_lines)
+ add_variant_to_order, automatically_fulfill_digital_lines,
+ cancel_fulfillment, cancel_order, change_order_line_quantity,
+ delete_order_line, fulfill_order_line, recalculate_order,
+ restock_fulfillment_lines, restock_order_lines, update_order_prices,
+ update_order_status)
from saleor.payment import ChargeStatus
from saleor.payment.models import Payment
from saleor.product.models import DigitalContent
-from tests.utils import get_redirect_location, create_image
+from tests.utils import create_image, get_redirect_location
def test_total_setter():
@@ -580,8 +582,10 @@ def test_get_order_weight_non_existing_product(order_with_lines, product):
assert old_weight == new_weight
+@patch('saleor.order.utils.emails.send_fulfillment_confirmation')
@patch('saleor.order.utils.get_default_digital_content_settings')
-def test_fulfill_digital_lines(mock_digital_settings, order_with_lines):
+def test_fulfill_digital_lines(
+ mock_digital_settings, mock_email_fulfillment, order_with_lines):
mock_digital_settings.return_value = {'automatic_fulfillment': True}
line = order_with_lines.lines.all()[0]
@@ -603,6 +607,7 @@ def test_fulfill_digital_lines(mock_digital_settings, order_with_lines):
assert fulfillment_lines.count() == 1
assert line.digital_content_url
+ assert mock_email_fulfillment.delay.called
def test_fulfill_order_line(order_with_lines):