Skip to content

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

Extend order line with total price (#6640)

Iga Karbowiakbbba01a02725
diff --git a/saleor/checkout/base_calculations.py b/saleor/checkout/base_calculations.py
index 0fa7d74dbd..96444fc8ae 100644
--- a/saleor/checkout/base_calculations.py
+++ b/saleor/checkout/base_calculations.py
@@ -80,3 +80,7 @@ def base_checkout_line_total(
amount = line.quantity * variant_price
price = quantize_price(amount, amount.currency)
return TaxedMoney(net=price, gross=price)
+
+
+def base_checkout_line_unit_price(total_line_price: TaxedMoney, quantity: int):
+ return total_line_price / quantity
diff --git a/saleor/checkout/complete_checkout.py b/saleor/checkout/complete_checkout.py
index 781daff1f8..bfe85ea54a 100644
--- a/saleor/checkout/complete_checkout.py
+++ b/saleor/checkout/complete_checkout.py
@@ -15,7 +15,6 @@ from ..account.utils import store_user_address
from ..checkout import calculations
from ..checkout.error_codes import CheckoutErrorCode
from ..core.exceptions import InsufficientStock
-from ..core.prices import quantize_price
from ..core.taxes import TaxError, zero_taxed_money
from ..core.utils.url import validate_storefront_url
from ..discount import DiscountInfo
@@ -181,9 +180,7 @@ def _create_line_for_order(
channel_listing,
discounts,
)
- unit_price = quantize_price(
- total_line_price / checkout_line.quantity, total_line_price.currency
- )
+ unit_price = manager.calculate_checkout_line_unit_price(total_line_price, quantity)
tax_rate = Decimal("0.0")
# The condition will return False when unit_price.gross is 0.0
if not isinstance(unit_price, Decimal) and unit_price.gross:
@@ -199,6 +196,7 @@ def _create_line_for_order(
quantity=quantity,
variant=variant,
unit_price=unit_price, # type: ignore
+ total_price=total_line_price,
tax_rate=tax_rate,
)
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.py
index d0f5fef292..9c2c41740d 100644
--- a/saleor/checkout/utils.py
+++ b/saleor/checkout/utils.py
@@ -11,7 +11,6 @@ from ..channel.models import Channel
from ..checkout import calculations
from ..checkout.error_codes import CheckoutErrorCode
from ..core.exceptions import ProductNotPublished
-from ..core.prices import quantize_price
from ..core.taxes import zero_taxed_money
from ..core.utils.promo_code import (
InvalidPromoCode,
@@ -312,8 +311,8 @@ def get_prices_of_discounted_specific_product(
channel_listing=line.variant.channel_listings.get(channel=channel),
discounts=discounts or [],
).gross
- line_unit_price = quantize_price(
- (line_total / line.quantity), line_total.currency
+ line_unit_price = manager.calculate_checkout_line_unit_price(
+ line_total, line.quantity
)
line_prices.extend([line_unit_price] * line.quantity)
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.py
index 915a08ef6e..0c11bf1816 100644
--- a/saleor/core/utils/random_data.py
+++ b/saleor/core/utils/random_data.py
@@ -581,6 +581,7 @@ def create_order_lines(order, discounts, how_many=10):
discounts,
)
unit_price = TaxedMoney(net=unit_price, gross=unit_price)
+ total_price = unit_price * quantity
lines.append(
OrderLine(
order=order,
@@ -591,6 +592,7 @@ def create_order_lines(order, discounts, how_many=10):
quantity=quantity,
variant=variant,
unit_price=unit_price,
+ total_price=total_price,
tax_rate=0,
)
)
@@ -681,7 +683,7 @@ def create_fake_order(discounts, max_order_lines=5):
order = Order.objects.create(**order_data)
lines = create_order_lines(order, discounts, random.randrange(1, max_order_lines))
- order.total = sum([line.get_total() for line in lines], shipping_price)
+ order.total = sum([line.total_price for line in lines], shipping_price)
weight = Weight(kg=0)
for line in order:
weight += line.variant.get_weight()
diff --git a/saleor/graphql/order/tests/test_fulfillment.py b/saleor/graphql/order/tests/test_fulfillment.py
index 4756ef9465..17d5ecc13e 100644
--- a/saleor/graphql/order/tests/test_fulfillment.py
+++ b/saleor/graphql/order/tests/test_fulfillment.py
@@ -1315,15 +1315,18 @@ def test_fulfillment_refund_products_fulfillment_lines_and_order_lines(
gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
variant.track_inventory = False
variant.save()
+ quantity = 5
+ total_price = TaxedMoney(net=net * quantity, gross=gross * quantity)
order_line = fulfilled_order.lines.create(
product_name=str(variant.product),
variant_name=str(variant),
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
- quantity=5,
+ quantity=quantity,
quantity_fulfilled=2,
variant=variant,
unit_price=TaxedMoney(net=net, gross=gross),
+ total_price=total_price,
tax_rate=23,
)
fulfillment = fulfilled_order.fulfillments.get()
diff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.py
index 55f52faa37..9e6c8eee14 100644
--- a/saleor/graphql/order/types.py
+++ b/saleor/graphql/order/types.py
@@ -304,7 +304,7 @@ class OrderLine(CountableDjangoObjectType):
@staticmethod
def resolve_total_price(root: models.OrderLine, _info):
- return root.unit_price * root.quantity
+ return root.total_price
@staticmethod
def resolve_translated_product_name(root: models.OrderLine, _info):
diff --git a/saleor/graphql/product/tests/test_bulk_delete.py b/saleor/graphql/product/tests/test_bulk_delete.py
index 155e4a30ce..84fb5536e1 100644
--- a/saleor/graphql/product/tests/test_bulk_delete.py
+++ b/saleor/graphql/product/tests/test_bulk_delete.py
@@ -196,7 +196,8 @@ def test_delete_products(
variant_channel_listing = variant.channel_listings.get(channel=channel_USD)
net = variant.get_price(product, [], channel_USD, variant_channel_listing, None)
gross = Money(amount=net.amount, currency=net.currency)
-
+ quantity = 3
+ total_price = TaxedMoney(net=net * quantity, gross=gross * quantity)
order_line = OrderLine.objects.create(
variant=variant,
order=draft_order,
@@ -205,6 +206,7 @@ def test_delete_products(
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
unit_price=TaxedMoney(net=net, gross=gross),
+ total_price=total_price,
quantity=3,
)
draft_order_lines_pks.append(order_line.pk)
@@ -217,6 +219,7 @@ def test_delete_products(
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
unit_price=TaxedMoney(net=net, gross=gross),
+ total_price=total_price,
quantity=3,
)
not_draft_order_lines_pks.append(order_line_not_draft.pk)
@@ -392,6 +395,9 @@ def test_delete_product_variants_in_draft_orders(
second_product, [], channel_USD, second_variant_channel_listing, None
)
gross = Money(amount=net.amount, currency=net.currency)
+ unit_price = TaxedMoney(net=net, gross=gross)
+ quantity = 3
+ total_price = unit_price * quantity
second_draft_order = OrderLine.objects.create(
variant=second_variant_in_draft,
order=draft_order,
@@ -400,7 +406,8 @@ def test_delete_product_variants_in_draft_orders(
product_sku=second_variant_in_draft.sku,
is_shipping_required=second_variant_in_draft.is_shipping_required(),
unit_price=TaxedMoney(net=net, gross=gross),
- quantity=3,
+ total_price=total_price,
+ quantity=quantity,
)
variant = variants[0]
@@ -408,6 +415,9 @@ def test_delete_product_variants_in_draft_orders(
variant_channel_listing = variant.channel_listings.get(channel=channel_USD)
net = variant.get_price(product, [], channel_USD, variant_channel_listing, None)
gross = Money(amount=net.amount, currency=net.currency)
+ unit_price = TaxedMoney(net=net, gross=gross)
+ quantity = 3
+ total_price = unit_price * quantity
order_not_draft = order_list[-1]
order_line_not_in_draft = OrderLine.objects.create(
variant=variant,
@@ -417,7 +427,8 @@ def test_delete_product_variants_in_draft_orders(
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
unit_price=TaxedMoney(net=net, gross=gross),
- quantity=3,
+ total_price=total_price,
+ quantity=quantity,
)
order_line_not_in_draft_pk = order_line_not_in_draft.pk
diff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.py
index 6cf66b27ae..7a809b0c2d 100644
--- a/saleor/graphql/product/tests/test_product.py
+++ b/saleor/graphql/product/tests/test_product.py
@@ -3862,6 +3862,9 @@ def test_delete_product_variant_in_draft_order(
variant_channel_listing = variant.channel_listings.get(channel=channel_USD)
net = variant.get_price(product, [], channel_USD, variant_channel_listing, None)
gross = Money(amount=net.amount, currency=net.currency)
+ unit_price = TaxedMoney(net=net, gross=gross)
+ quantity = 3
+ total_price = unit_price * quantity
order_line = OrderLine.objects.create(
variant=variant,
@@ -3871,7 +3874,8 @@ def test_delete_product_variant_in_draft_order(
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
unit_price=TaxedMoney(net=net, gross=gross),
- quantity=3,
+ total_price=total_price,
+ quantity=quantity,
)
draft_order_lines_pks.append(order_line.pk)
@@ -3883,7 +3887,8 @@ def test_delete_product_variant_in_draft_order(
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
unit_price=TaxedMoney(net=net, gross=gross),
- quantity=3,
+ total_price=total_price,
+ quantity=quantity,
)
not_draft_order_lines_pks.append(order_line_not_draft.pk)
@@ -4692,6 +4697,9 @@ def test_product_type_delete_mutation_variants_in_draft_order(
variant_channel_listing = variant.channel_listings.get(channel=channel_USD)
net = variant.get_price(product, [], channel_USD, variant_channel_listing, None)
gross = Money(amount=net.amount, currency=net.currency)
+ quantity = 3
+ unit_price = TaxedMoney(net=net, gross=gross)
+ total_price = unit_price * quantity
order_line_not_in_draft = OrderLine.objects.create(
variant=variant,
@@ -4701,6 +4709,7 @@ def test_product_type_delete_mutation_variants_in_draft_order(
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
unit_price=TaxedMoney(net=net, gross=gross),
+ total_price=total_price,
quantity=3,
)
@@ -4712,6 +4721,7 @@ def test_product_type_delete_mutation_variants_in_draft_order(
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
unit_price=TaxedMoney(net=net, gross=gross),
+ total_price=total_price,
quantity=3,
)
diff --git a/saleor/graphql/product/tests/test_variant.py b/saleor/graphql/product/tests/test_variant.py
index 75a9d929c4..3b899092e6 100644
--- a/saleor/graphql/product/tests/test_variant.py
+++ b/saleor/graphql/product/tests/test_variant.py
@@ -1396,6 +1396,8 @@ def test_delete_variant_in_draft_order(
net = variant.get_price(product, [], channel_USD, variant_channel_listing, None)
gross = Money(amount=net.amount, currency=net.currency)
order_not_draft = order_list[-1]
+ unit_price = TaxedMoney(net=net, gross=gross)
+ quantity = 3
order_line_not_in_draft = OrderLine.objects.create(
variant=variant,
order=order_not_draft,
@@ -1403,8 +1405,9 @@ def test_delete_variant_in_draft_order(
variant_name=str(variant),
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
- unit_price=TaxedMoney(net=net, gross=gross),
- quantity=3,
+ unit_price=unit_price,
+ total_price=unit_price * quantity,
+ quantity=quantity,
)
order_line_not_in_draft_pk = order_line_not_in_draft.pk
diff --git a/saleor/order/migrations/0094_auto_20201221_1128.py b/saleor/order/migrations/0094_auto_20201221_1128.py
new file mode 100644
index 0000000000..9c15666c08
--- /dev/null
+++ b/saleor/order/migrations/0094_auto_20201221_1128.py
@@ -0,0 +1,53 @@
+# Generated by Django 3.1.3 on 2020-12-21 11:28
+
+from django.db import migrations, models
+
+
+def set_total_prices(apps, schema_editor):
+ OrderLine = apps.get_model("order", "OrderLine")
+ lines = []
+ for line in OrderLine.objects.filter(
+ total_price_gross_amount__isnull=True
+ ).iterator():
+ line.total_price_gross_amount = line.unit_price_gross_amount * line.quantity
+ line.total_price_net_amount = line.unit_price_net_amount * line.quantity
+ lines.append(line)
+
+ OrderLine.objects.bulk_update(
+ lines, ["total_price_gross_amount", "total_price_net_amount"], batch_size=1000
+ )
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("order", "0093_auto_20201130_1406"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="orderline",
+ name="total_price_gross_amount",
+ field=models.DecimalField(
+ blank=True, decimal_places=3, max_digits=12, null=True
+ ),
+ ),
+ migrations.AddField(
+ model_name="orderline",
+ name="total_price_net_amount",
+ field=models.DecimalField(
+ blank=True, decimal_places=3, max_digits=12, null=True
+ ),
+ ),
+ migrations.RunPython(set_total_prices, migrations.RunPython.noop,),
+ migrations.AlterField(
+ model_name="orderline",
+ name="total_price_gross_amount",
+ field=models.DecimalField(decimal_places=3, max_digits=12),
+ ),
+ migrations.AlterField(
+ model_name="orderline",
+ name="total_price_net_amount",
+ field=models.DecimalField(decimal_places=3, max_digits=12),
+ ),
+ ]
diff --git a/saleor/order/models.py b/saleor/order/models.py
index 3c69a3d679..dec1e0325a 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -278,7 +278,7 @@ class Order(ModelWithMetadata):
return any(line.is_shipping_required for line in self)
def get_subtotal(self):
- subtotal_iterator = (line.get_total() for line in self)
+ subtotal_iterator = (line.total_price for line in self)
return sum(subtotal_iterator, zero_taxed_money(currency=self.currency))
def get_total_quantity(self):
@@ -408,6 +408,28 @@ class OrderLine(models.Model):
currency="currency",
)
+ total_price_net_amount = models.DecimalField(
+ max_digits=settings.DEFAULT_MAX_DIGITS,
+ decimal_places=settings.DEFAULT_DECIMAL_PLACES,
+ )
+ total_price_net = MoneyField(
+ amount_field="total_price_net_amount", currency_field="currency",
+ )
+
+ total_price_gross_amount = models.DecimalField(
+ max_digits=settings.DEFAULT_MAX_DIGITS,
+ decimal_places=settings.DEFAULT_DECIMAL_PLACES,
+ )
+ total_price_gross = MoneyField(
+ amount_field="total_price_gross_amount", currency_field="currency",
+ )
+
+ total_price = TaxedMoneyField(
+ net_amount_field="total_price_net_amount",
+ gross_amount_field="total_price_gross_amount",
+ currency="currency",
+ )
+
tax_rate = models.DecimalField(
max_digits=5, decimal_places=2, default=Decimal("0.0")
)
@@ -424,9 +446,6 @@ class OrderLine(models.Model):
else self.product_name
)
- def get_total(self):
- return self.unit_price * self.quantity
-
@property
def quantity_unfulfilled(self):
return self.quantity - self.quantity_fulfilled
diff --git a/saleor/order/tests/test_order.py b/saleor/order/tests/test_order.py
index e1a8991650..b48a24b627 100644
--- a/saleor/order/tests/test_order.py
+++ b/saleor/order/tests/test_order.py
@@ -420,7 +420,7 @@ def test_update_order_prices(order_with_lines):
assert line_1.unit_price == price_1
assert line_2.unit_price == price_2
assert order_with_lines.shipping_price == shipping_price
- total = line_1.quantity * price_1 + line_2.quantity * price_2 + shipping_price
+ total = line_1.total_price + line_2.total_price + shipping_price
assert order_with_lines.total == total
diff --git a/saleor/order/tests/test_order_actions.py b/saleor/order/tests/test_order_actions.py
index 8ed093bf20..6f5704dd4e 100644
--- a/saleor/order/tests/test_order_actions.py
+++ b/saleor/order/tests/test_order_actions.py
@@ -44,6 +44,7 @@ def order_with_digital_line(order, digital_content, stock, site_settings):
variant_channel_listing = variant.channel_listings.get(channel=channel)
net = variant.get_price(product, [], channel, variant_channel_listing, None)
gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
+ unit_price = TaxedMoney(net=net, gross=gross)
line = order.lines.create(
product_name=str(product),
variant_name=str(variant),
@@ -51,7 +52,8 @@ def order_with_digital_line(order, digital_content, stock, site_settings):
is_shipping_required=variant.is_shipping_required(),
quantity=quantity,
variant=variant,
- unit_price=TaxedMoney(net=net, gross=gross),
+ unit_price=unit_price,
+ total_price=unit_price * quantity,
tax_rate=23,
)
@@ -601,16 +603,20 @@ def test_create_refund_fulfillment_multiple_refunds(
net = variant.get_price(
variant.product, [collection], channel_USD, variant_channel_listing, []
)
- gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
+ currency = net.currency
+ gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
+ quantity = 5
+ unit_price = TaxedMoney(net=net, gross=gross)
order_line = fulfilled_order.lines.create(
product_name=str(variant.product),
variant_name=str(variant),
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
- quantity=5,
+ quantity=quantity,
quantity_fulfilled=2,
variant=variant,
- unit_price=TaxedMoney(net=net, gross=gross),
+ unit_price=unit_price,
+ total_price=unit_price * quantity,
tax_rate=23,
)
Allocation.objects.create(
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index 31456862f2..12909fa44d 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -82,7 +82,7 @@ def recalculate_order(order: Order, **kwargs):
"""
# avoid using prefetched order lines
lines = [OrderLine.objects.get(pk=line.pk) for line in order]
- prices = [line.get_total() for line in lines]
+ prices = [line.total_price for line in lines]
total = sum(prices, order.shipping_price)
# discount amount can't be greater than order total
order.discount_amount = min(order.discount_amount, total.gross.amount)
@@ -188,6 +188,7 @@ def add_variant_to_draft_order(order, variant, quantity, discounts=None):
product, collections, channel, channel_listing, discounts
)
unit_price = TaxedMoney(net=unit_price, gross=unit_price)
+ total_price = unit_price * quantity
product_name = str(product)
variant_name = str(variant)
translated_product_name = str(product.translated)
@@ -205,6 +206,7 @@ def add_variant_to_draft_order(order, variant, quantity, discounts=None):
is_shipping_required=variant.is_shipping_required(),
quantity=quantity,
unit_price=unit_price,
+ total_price=total_price,
variant=variant,
)
manager = get_plugins_manager()
diff --git a/saleor/plugins/base_plugin.py b/saleor/plugins/base_plugin.py
index abf5f918be..2acebba376 100644
--- a/saleor/plugins/base_plugin.py
+++ b/saleor/plugins/base_plugin.py
@@ -168,6 +168,12 @@ class BasePlugin:
"""
return NotImplemented
+ def calculate_checkout_line_unit_price(
+ self, total_line_price: TaxedMoney, quantity: int, previous_value: TaxedMoney
+ ):
+ """Calculate checkout line unit price."""
+ return NotImplemented
+
def calculate_order_line_unit(
self, order_line: "OrderLine", previous_value: TaxedMoney
) -> TaxedMoney:
diff --git a/saleor/plugins/manager.py b/saleor/plugins/manager.py
index c04acbf052..ebf04804ce 100644
--- a/saleor/plugins/manager.py
+++ b/saleor/plugins/manager.py
@@ -247,6 +247,22 @@ class PluginsManager(PaymentInterface):
checkout.currency,
)
+ def calculate_checkout_line_unit_price(
+ self, total_line_price: TaxedMoney, quantity: int
+ ):
+ default_value = base_calculations.base_checkout_line_unit_price(
+ total_line_price, quantity
+ )
+ return quantize_price(
+ self.__run_method_on_plugins(
+ "calculate_checkout_line_unit_price",
+ default_value,
+ total_line_price,
+ quantity,
+ ),
+ total_line_price.currency,
+ )
+
def calculate_order_line_unit(self, order_line: "OrderLine") -> TaxedMoney:
unit_price = order_line.unit_price
default_value = quantize_price(unit_price, unit_price.currency)
diff --git a/saleor/plugins/tests/sample_plugins.py b/saleor/plugins/tests/sample_plugins.py
index 4dc9f91286..dbff13d61c 100644
--- a/saleor/plugins/tests/sample_plugins.py
+++ b/saleor/plugins/tests/sample_plugins.py
@@ -99,6 +99,11 @@ class PluginSample(BasePlugin):
price = Money("1.0", currency=checkout_line.checkout.currency)
return TaxedMoney(price, price)
+ def calculate_checkout_line_unit_price(
+ self, total_line_price: TaxedMoney, quantity: int, previous_value: TaxedMoney
+ ):
+ return total_line_price / quantity
+
def calculate_order_line_unit(self, order_line, previous_value):
currency = order_line.unit_price.currency
price = Money("1.0", currency)
diff --git a/saleor/plugins/tests/test_manager.py b/saleor/plugins/tests/test_manager.py
index b07cfb66fb..6971649aea 100644
--- a/saleor/plugins/tests/test_manager.py
+++ b/saleor/plugins/tests/test_manager.py
@@ -123,6 +123,43 @@ def test_manager_calculates_checkout_line_total(
assert TaxedMoney(expected_total, expected_total) == taxed_total
+ "plugins, total_line_price, quantity",
+ [
+ (
+ ["saleor.plugins.tests.sample_plugins.PluginSample"],
+ TaxedMoney(
+ net=Money(amount=10, currency="USD"),
+ gross=Money(amount=12, currency="USD"),
+ ),
+ 2,
+ ),
+ (
+ [],
+ TaxedMoney(
+ net=Money(amount=15, currency="USD"),
+ gross=Money(amount=15, currency="USD"),
+ ),
+ 1,
+ ),
+ ],
+)
+def test_manager_calculates_checkout_line_unit_price(
+ plugins, total_line_price, quantity
+):
+ taxed_total = PluginsManager(plugins=plugins).calculate_checkout_line_unit_price(
+ total_line_price, quantity
+ )
+ currency = total_line_price.net.currency
+ expected_net = Money(
+ amount=total_line_price.net.amount / quantity, currency=currency
+ )
+ expected_gross = Money(
+ amount=total_line_price.gross.amount / quantity, currency=currency
+ )
+ assert TaxedMoney(net=expected_net, gross=expected_gross) == taxed_total
+
+
@pytest.mark.parametrize(
"plugins, amount",
[(["saleor.plugins.tests.sample_plugins.PluginSample"], "1.0"), ([], "12.30")],
diff --git a/saleor/product/utils/__init__.py b/saleor/product/utils/__init__.py
index 3fd0e8d5a7..856a53948b 100644
--- a/saleor/product/utils/__init__.py
+++ b/saleor/product/utils/__init__.py
@@ -28,9 +28,7 @@ def calculate_revenue_for_variant(
for order_line in order_lines:
order = orders_dict[order_line.order_id]
if order.created >= start_date:
- net = order_line.unit_price_net * order_line.quantity
- gross = order_line.unit_price_gross * order_line.quantity
- revenue += TaxedMoney(net, gross)
+ revenue += order_line.total_price
return revenue
diff --git a/saleor/seo/schema/email.py b/saleor/seo/schema/email.py
index 4d4771170c..aca921cde2 100644
--- a/saleor/seo/schema/email.py
+++ b/saleor/seo/schema/email.py
@@ -16,7 +16,7 @@ def get_organization():
def get_product_data(line: "OrderLine", organization: dict) -> dict:
- gross_product_price = line.get_total().gross
+ gross_product_price = line.total_price.gross
line_name = str(line)
if line.translated_product_name:
line_name = (
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index 59bd693793..36a7b1de17 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -1936,15 +1936,19 @@ def order_line(order, variant):
channel = order.channel
channel_listing = variant.channel_listings.get(channel=channel)
net = variant.get_price(product, [], channel, channel_listing)
- gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
+ currency = net.currency
+ gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
+ quantity = 3
+ unit_price = TaxedMoney(net=net, gross=gross)
return order.lines.create(
product_name=str(product),
variant_name=str(variant),
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
- quantity=3,
+ quantity=quantity,
variant=variant,
- unit_price=TaxedMoney(net=net, gross=gross),
+ unit_price=unit_price,
+ total_price=unit_price * quantity,
tax_rate=23,
)
@@ -1967,15 +1971,19 @@ def order_line_with_allocation_in_many_stocks(
product = variant.product
channel_listing = variant.channel_listings.get(channel=channel_USD)
net = variant.get_price(product, [], channel_USD, channel_listing)
- gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
+ currency = net.currency
+ gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
+ quantity = 3
+ unit_price = TaxedMoney(net=net, gross=gross)
order_line = order.lines.create(
product_name=str(product),
variant_name=str(variant),
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
- quantity=3,
+ quantity=quantity,
variant=variant,
- unit_price=TaxedMoney(net=net, gross=gross),
+ unit_price=unit_price,
+ total_price=unit_price * quantity,
tax_rate=23,
)
@@ -2007,15 +2015,19 @@ def order_line_with_one_allocation(
product = variant.product
channel_listing = variant.channel_listings.get(channel=channel_USD)
net = variant.get_price(product, [], channel_USD, channel_listing)
- gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
+ currency = net.currency
+ gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
+ quantity = 2
+ unit_price = TaxedMoney(net=net, gross=gross)
order_line = order.lines.create(
product_name=str(product),
variant_name=str(variant),
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
- quantity=2,
+ quantity=quantity,
variant=variant,
- unit_price=TaxedMoney(net=net, gross=gross),
+ unit_price=unit_price,
+ total_price=unit_price * quantity,
tax_rate=Decimal(23),
)
@@ -2083,15 +2095,19 @@ def order_with_lines(
warehouse=warehouse, product_variant=variant, quantity=5
)
net = variant.get_price(product, [], channel_USD, channel_listing)
- gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
+ currency = net.currency
+ gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
+ quantity = 3
+ unit_price = TaxedMoney(net=net, gross=gross)
line = order.lines.create(
product_name=str(variant.product),
variant_name=str(variant),
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
- quantity=3,
+ quantity=quantity,
variant=variant,
- unit_price=TaxedMoney(net=net, gross=gross),
+ unit_price=unit_price,
+ total_price=unit_price * quantity,
tax_rate=23,
)
Allocation.objects.create(
@@ -2124,15 +2140,19 @@ def order_with_lines(
)
net = variant.get_price(product, [], channel_USD, channel_listing)
- gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
+ currency = net.currency
+ gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
+ unit_price = TaxedMoney(net=net, gross=gross)
+ quantity = 2
line = order.lines.create(
product_name=str(variant.product),
variant_name=str(variant),
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
- quantity=2,
+ quantity=quantity,
variant=variant,
- unit_price=TaxedMoney(net=net, gross=gross),
+ unit_price=unit_price,
+ total_price=unit_price * quantity,
tax_rate=23,
)
Allocation.objects.create(
@@ -2199,15 +2219,19 @@ def order_with_lines_channel_PLN(
warehouse=warehouse, product_variant=variant, quantity=5
)
net = variant.get_price(product, [], channel_PLN, channel_listing)
- gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
+ currency = net.currency
+ gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
+ quantity = 3
+ unit_price = TaxedMoney(net=net, gross=gross)
line = order.lines.create(
product_name=str(variant.product),
variant_name=str(variant),
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
- quantity=3,
+ quantity=quantity,
variant=variant,
- unit_price=TaxedMoney(net=net, gross=gross),
+ unit_price=unit_price,
+ total_price=unit_price * quantity,
tax_rate=23,
)
Allocation.objects.create(
@@ -2240,15 +2264,19 @@ def order_with_lines_channel_PLN(
)
net = variant.get_price(product, [], channel_PLN, channel_listing, None)
- gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
+ currency = net.currency
+ gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
+ quantity = 2
+ unit_price = TaxedMoney(net=net, gross=gross)
line = order.lines.create(
product_name=str(variant.product),
variant_name=str(variant),
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
- quantity=2,
+ quantity=quantity,
variant=variant,
- unit_price=TaxedMoney(net=net, gross=gross),
+ unit_price=unit_price,
+ total_price=unit_price * quantity,
tax_rate=23,
)
Allocation.objects.create(
@@ -2282,15 +2310,19 @@ def order_with_line_without_inventory_tracking(
channel = order.channel
channel_listing = variant.channel_listings.get(channel=channel)
net = variant.get_price(product, [], channel, channel_listing)
- gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
+ currency = net.currency
+ gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
+ quantity = 3
+ unit_price = TaxedMoney(net=net, gross=gross)
line = order.lines.create(
product_name=str(variant.product),
variant_name=str(variant),
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
- quantity=3,
+ quantity=quantity,
variant=variant,
- unit_price=TaxedMoney(net=net, gross=gross),
+ unit_price=unit_price,
+ total_price=unit_price * quantity,
tax_rate=23,
)
@@ -3324,6 +3356,7 @@ def allocations(order_list, stock, channel_USD):
channel_listing = variant.channel_listings.get(channel=channel_USD)
net = variant.get_price(product, [], channel_USD, channel_listing)
gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
+ price = TaxedMoney(net=net, gross=gross)
lines = OrderLine.objects.bulk_create(
[
OrderLine(
@@ -3334,7 +3367,8 @@ def allocations(order_list, stock, channel_USD):
variant_name=str(variant),
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
- unit_price=TaxedMoney(net=net, gross=gross),
+ unit_price=price,
+ total_price=price,
tax_rate=23,
),
OrderLine(
@@ -3345,7 +3379,8 @@ def allocations(order_list, stock, channel_USD):
variant_name=str(variant),
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
- unit_price=TaxedMoney(net=net, gross=gross),
+ unit_price=price,
+ total_price=price,
tax_rate=23,
),
OrderLine(
@@ -3356,7 +3391,8 @@ def allocations(order_list, stock, channel_USD):
variant_name=str(variant),
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
- unit_price=TaxedMoney(net=net, gross=gross),
+ unit_price=price,
+ total_price=price,
tax_rate=23,
),
]
diff --git a/saleor/webhook/payloads.py b/saleor/webhook/payloads.py
index 9797855ef2..14bb5b810b 100644
--- a/saleor/webhook/payloads.py
+++ b/saleor/webhook/payloads.py
@@ -66,6 +66,8 @@ def generate_order_lines_payload(lines: Iterable[OrderLine]):
"currency",
"unit_price_net_amount",
"unit_price_gross_amount",
+ "total_price_net_amount",
+ "total_price_gross_amount",
"tax_rate",
)
serializer = PayloadSerializer()
@@ -73,8 +75,8 @@ def generate_order_lines_payload(lines: Iterable[OrderLine]):
lines,
fields=line_fields,
extra_dict_data={
- "total_price_net_amount": (lambda l: l.get_total().net.amount),
- "total_price_gross_amount": (lambda l: l.get_total().gross.amount),
+ "total_price_net_amount": (lambda l: l.total_price.net.amount),
+ "total_price_gross_amount": (lambda l: l.total_price.gross.amount),
},
)
diff --git a/saleor/webhook/tests/test_webhook_payloads.py b/saleor/webhook/tests/test_webhook_payloads.py
index 0ce862d58e..157f4c97fa 100644
--- a/saleor/webhook/tests/test_webhook_payloads.py
+++ b/saleor/webhook/tests/test_webhook_payloads.py
@@ -38,7 +38,7 @@ def test_order_lines_have_all_required_fields(order, order_line_with_one_allocat
line_payload = lines_payload[0]
unit_net_amount = line.unit_price_net_amount.quantize(Decimal("0.001"))
unit_gross_amount = line.unit_price_gross_amount.quantize(Decimal("0.001"))
- total_line = line.get_total()
+ total_line = line.total_price
assert line_payload == {
"id": line_id,
"type": "OrderLine",
diff --git a/templates/templated_email/compiled/confirm_order.html b/templates/templated_email/compiled/confirm_order.html
index fb30a47675..efa955dc7b 100644
--- a/templates/templated_email/compiled/confirm_order.html
+++ b/templates/templated_email/compiled/confirm_order.html
@@ -243,7 +243,7 @@
<td style="padding: 5px; text-align: right;" align="right">
{% price line.unit_price display_gross=order.display_gross_prices html=False %}
</td>
- <td style="padding: 5px; text-align: right;" align="right">{% price line.get_total display_gross=order.display_gross_prices html=False %}</td>
+ <td style="padding: 5px; text-align: right;" align="right">{% price line.total_price display_gross=order.display_gross_prices html=False %}</td>
</tr>
{% endfor %}
</tbody>
diff --git a/templates/templated_email/compiled/confirmed_order.html b/templates/templated_email/compiled/confirmed_order.html
index e20ce5e41e..260277abf7 100644
--- a/templates/templated_email/compiled/confirmed_order.html
+++ b/templates/templated_email/compiled/confirmed_order.html
@@ -243,7 +243,7 @@
<td style="padding: 5px; text-align: right;" align="right">
{% price line.unit_price display_gross=order.display_gross_prices html=False %}
</td>
- <td style="padding: 5px; text-align: right;" align="right">{% price line.get_total display_gross=order.display_gross_prices html=False %}</td>
+ <td style="padding: 5px; text-align: right;" align="right">{% price line.total_price display_gross=order.display_gross_prices html=False %}</td>
</tr>
{% endfor %}
</tbody>
diff --git a/templates/templated_email/compiled/staff_confirm_order.html b/templates/templated_email/compiled/staff_confirm_order.html
index c9d982b941..756d4388db 100644
--- a/templates/templated_email/compiled/staff_confirm_order.html
+++ b/templates/templated_email/compiled/staff_confirm_order.html
@@ -237,7 +237,7 @@
<td style="padding: 5px; text-align: right;" align="right">
{% price line.unit_price display_gross=order.display_gross_prices html=False %}
</td>
- <td style="padding: 5px; text-align: right;" align="right">{% price line.get_total display_gross=order.display_gross_prices html=False %}</td>
+ <td style="padding: 5px; text-align: right;" align="right">{% price line.total_price display_gross=order.display_gross_prices html=False %}</td>
</tr>
{% endfor %}
</tbody>
diff --git a/templates/templated_email/order/confirm_order.email b/templates/templated_email/order/confirm_order.email
index d1141c6af7..266136df9d 100644
--- a/templates/templated_email/order/confirm_order.email
+++ b/templates/templated_email/order/confirm_order.email
@@ -20,7 +20,7 @@ To see your payment details please visit:
{% endblocktrans %}
{% endif %}
{% for line in order %}
-{{ line.quantity }} x {{ line }}, {% price line.get_total display_gross=order.display_gross_prices html=False %}
+{{ line.quantity }} x {{ line }}, {% price line.total_price display_gross=order.display_gross_prices html=False %}
{% endfor %}
{% trans "Order summary" context "Order confirmation e-mail table header" %}:
{% trans "Subtotal" context "Order confirmation e-mail table header" %}: {% price order.get_subtotal display_gross=order.display_gross_prices html=False %}
diff --git a/templates/templated_email/order/confirmed_order.email b/templates/templated_email/order/confirmed_order.email
index 8c957b561a..bea3915d16 100644
--- a/templates/templated_email/order/confirmed_order.email
+++ b/templates/templated_email/order/confirmed_order.email
@@ -20,7 +20,7 @@ To see your order details please visit:
{% endblocktrans %}
{% endif %}
{% for line in order %}
-{{ line.quantity }} x {{ line }}, {% price line.get_total display_gross=order.display_gross_prices html=False %}
+{{ line.quantity }} x {{ line }}, {% price line.total_price display_gross=order.display_gross_prices html=False %}
{% endfor %}
{% trans "Order summary" context "Order confirmed e-mail table header" %}:
{% trans "Subtotal" context "Order confirmed e-mail table header" %}: {% price order.get_subtotal display_gross=order.display_gross_prices html=False %}
diff --git a/templates/templated_email/order/staff_confirm_order.email b/templates/templated_email/order/staff_confirm_order.email
index 689d8731a4..79f79a410d 100644
--- a/templates/templated_email/order/staff_confirm_order.email
+++ b/templates/templated_email/order/staff_confirm_order.email
@@ -20,7 +20,7 @@ To see your payment details please visit:
{% endblocktrans %}
{% endif %}
{% for line in order %}
-{{ line.quantity }} x {{ line }}, {% price line.get_total display_gross=order.display_gross_prices html=False %}
+{{ line.quantity }} x {{ line }}, {% price line.total_price display_gross=order.display_gross_prices html=False %}
{% endfor %}
{% trans "Order summary" context "Order confirmation e-mail table header" %}:
{% trans "Subtotal" context "Order confirmation e-mail table header" %}: {% price order.get_subtotal display_gross=order.display_gross_prices html=False %}
diff --git a/templates/templated_email/source/partials/_order_lines.mjml b/templates/templated_email/source/partials/_order_lines.mjml
index a1cdb3b838..ba8cf49903 100644
--- a/templates/templated_email/source/partials/_order_lines.mjml
+++ b/templates/templated_email/source/partials/_order_lines.mjml
@@ -70,7 +70,7 @@
<td style="text-align: right;">
{% price line.unit_price display_gross=order.display_gross_prices html=False %}
</td>
- <td style="text-align: right;">{% price line.get_total display_gross=order.display_gross_prices html=False %}</td>
+ <td style="text-align: right;">{% price line.total_price display_gross=order.display_gross_prices html=False %}</td>
</tr>
{% endfor %}
</tbody>