Skip to content

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

Add variant name to order line

fowczarek31c1e9c9cbf1
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.py
index 8a49844b9c..23feed7dfe 100644
--- a/saleor/checkout/utils.py
+++ b/saleor/checkout/utils.py
@@ -1028,14 +1028,21 @@ def create_line_for_order(checkout_line: "CheckoutLine", discounts) -> OrderLine
quantity = checkout_line.quantity
variant = checkout_line.variant
+ product = variant.product
variant.check_quantity(quantity)
- product_name = variant.display_product()
- translated_product_name = variant.display_product(translated=True)
+ product_name = str(product)
+ variant_name = str(variant)
+
+ translated_product_name = str(product.translated)
+ translated_variant_name = str(variant.translated)
if translated_product_name == product_name:
translated_product_name = ""
+ if translated_variant_name == variant_name:
+ translated_variant_name = ""
+
manager = get_extensions_manager()
total_line_price = manager.calculate_checkout_line_total(checkout_line, discounts)
unit_price = quantize_price(
@@ -1043,7 +1050,9 @@ def create_line_for_order(checkout_line: "CheckoutLine", discounts) -> OrderLine
)
line = OrderLine(
product_name=product_name,
+ variant_name=variant_name,
translated_product_name=translated_product_name,
+ translated_variant_name=translated_variant_name,
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
quantity=quantity,
diff --git a/saleor/core/analytics.py b/saleor/core/analytics.py
index 448039113e..e266eb8d07 100644
--- a/saleor/core/analytics.py
+++ b/saleor/core/analytics.py
@@ -40,10 +40,7 @@ def _report(client_id, payloads, extra_headers=None, **extra_data):
def get_order_payloads(order):
items = [
ga.item(
- ol.product_name,
- ol.unit_price.gross,
- quantity=ol.quantity,
- item_id=ol.product_sku,
+ str(ol), ol.unit_price.gross, quantity=ol.quantity, item_id=ol.product_sku
)
for ol in order
]
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.py
index 72c5dfb223..45ab0fb7c5 100644
--- a/saleor/core/utils/random_data.py
+++ b/saleor/core/utils/random_data.py
@@ -359,6 +359,7 @@ def create_order_lines(order, discounts, how_many=10):
lines = []
for dummy in range(how_many):
variant = next(variants_iter)
+ product = variant.product
quantity = random.randrange(1, 5)
variant.quantity += quantity
variant.quantity_allocated += quantity
@@ -367,7 +368,8 @@ def create_order_lines(order, discounts, how_many=10):
lines.append(
OrderLine(
order=order,
- product_name=variant.display_product(),
+ product_name=str(product),
+ variant_name=str(variant),
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
quantity=quantity,
diff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.py
index 39c9e2a9d1..520e730c7e 100644
--- a/saleor/graphql/order/types.py
+++ b/saleor/graphql/order/types.py
@@ -208,11 +208,13 @@ class OrderLine(CountableDjangoObjectType):
"id",
"is_shipping_required",
"product_name",
+ "variant_name",
"product_sku",
"quantity",
"quantity_fulfilled",
"tax_rate",
"translated_product_name",
+ "translated_variant_name",
]
@staticmethod
diff --git a/saleor/order/migrations/0073_auto_20190829_0249.py b/saleor/order/migrations/0073_auto_20190829_0249.py
new file mode 100644
index 0000000000..27783790bc
--- /dev/null
+++ b/saleor/order/migrations/0073_auto_20190829_0249.py
@@ -0,0 +1,34 @@
+# Generated by Django 2.2.4 on 2019-08-29 07:49
+
+from django.db import migrations, models
+
+
+def add_variant_name_to_order_line(apps, schema_editor):
+ cls = apps.get_model("order", "OrderLine")
+ order_lines = cls.objects.all()
+
+ for order_line in order_lines:
+ if order_line.variant:
+ variant_name = order_line.variant.name or order_line.variant.sku
+ order_line.variant_name = variant_name
+ order_line.product_name = order_line.variant.product.name
+ order_line.save(update_fields=["variant_name", "product_name"])
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [("order", "0072_django_price_2")]
+
+ operations = [
+ migrations.AddField(
+ model_name="orderline",
+ name="translated_variant_name",
+ field=models.CharField(blank=True, default="", max_length=255),
+ ),
+ migrations.AddField(
+ model_name="orderline",
+ name="variant_name",
+ field=models.CharField(blank=True, default="", max_length=255),
+ ),
+ migrations.RunPython(add_variant_name_to_order_line),
+ ]
diff --git a/saleor/order/models.py b/saleor/order/models.py
index ad3e54392c..fa02ee834f 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -360,7 +360,9 @@ class OrderLine(models.Model):
)
# max_length is as produced by ProductVariant's display_product method
product_name = models.CharField(max_length=386)
+ variant_name = models.CharField(max_length=255, default="", blank=True)
translated_product_name = models.CharField(max_length=386, default="", blank=True)
+ translated_variant_name = models.CharField(max_length=255, default="", blank=True)
product_sku = models.CharField(max_length=32)
is_shipping_required = models.BooleanField()
quantity = models.IntegerField(validators=[MinValueValidator(1)])
@@ -405,7 +407,11 @@ class OrderLine(models.Model):
ordering = ("pk",)
def __str__(self):
- return self.product_name
+ return (
+ "%s (%s)" % (self.product_name, self.variant_name)
+ if self.variant_name
+ else self.product_name
+ )
def get_total(self):
return self.unit_price * self.quantity
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index 1aca17048c..b76f5017f5 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -289,13 +289,20 @@ def add_variant_to_order(
except OrderLine.DoesNotExist:
unit_price = variant.get_price(discounts)
unit_price = TaxedMoney(net=unit_price, gross=unit_price)
- product_name = variant.display_product()
- translated_product_name = variant.display_product(translated=True)
+ product = variant.product
+ product_name = str(product)
+ variant_name = str(variant)
+ translated_product_name = str(product.translated)
+ translated_variant_name = str(variant.translated)
if translated_product_name == product_name:
translated_product_name = ""
+ if translated_variant_name == variant_name:
+ translated_variant_name = ""
line = order.lines.create(
product_name=product_name,
+ variant_name=variant_name,
translated_product_name=translated_product_name,
+ translated_variant_name=translated_variant_name,
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
quantity=quantity,
diff --git a/saleor/seo/schema/email.py b/saleor/seo/schema/email.py
index e725a227bc..841aae0c4f 100644
--- a/saleor/seo/schema/email.py
+++ b/saleor/seo/schema/email.py
@@ -13,13 +13,16 @@ def get_organization():
def get_product_data(line, organization):
gross_product_price = line.get_total().gross
+ line_name = str(line)
+ if line.translated_product_name:
+ line_name = (
+ "%s (%s)" % (line.translated_product_name, line.translated_variant_name)
+ if line.translated_variant_name
+ else line.translated_product_name
+ )
product_data = {
"@type": "Offer",
- "itemOffered": {
- "@type": "Product",
- "name": line.translated_product_name or line.product_name,
- "sku": line.product_sku,
- },
+ "itemOffered": {"@type": "Product", "name": line_name, "sku": line.product_sku},
"price": gross_product_price.amount,
"priceCurrency": gross_product_price.currency,
"eligibleQuantity": {"@type": "QuantitativeValue", "value": line.quantity},
diff --git a/tests/api/test_order.py b/tests/api/test_order.py
index 49c6190187..c0045a6187 100644
--- a/tests/api/test_order.py
+++ b/tests/api/test_order.py
@@ -324,6 +324,7 @@ def test_nested_order_events_query(
quantity
orderLine {
productName
+ variantName
}
}
paymentId
@@ -367,7 +368,10 @@ def test_nested_order_events_query(
assert data["fulfilledItems"] == [
{
"quantity": line.quantity,
- "orderLine": {"productName": line.order_line.product_name},
+ "orderLine": {
+ "productName": line.order_line.product_name,
+ "variantName": line.order_line.variant_name,
+ },
}
for line in fulfillment.lines.all()
]
diff --git a/tests/conftest.py b/tests/conftest.py
index 636b6223c4..9dcf9e85d6 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -751,7 +751,8 @@ def order_line(order, variant):
net = variant.get_price()
gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
return order.lines.create(
- product_name=variant.display_product(),
+ product_name=str(variant.product),
+ variant_name=str(variant),
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
quantity=3,
@@ -807,7 +808,8 @@ def order_with_lines(order, product_type, category, shipping_zone):
net = variant.get_price()
gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
order.lines.create(
- product_name=variant.display_product(),
+ product_name=str(variant.product),
+ variant_name=str(variant),
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
quantity=3,
@@ -833,7 +835,8 @@ def order_with_lines(order, product_type, category, shipping_zone):
net = variant.get_price()
gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
order.lines.create(
- product_name=variant.display_product(),
+ product_name=str(variant.product),
+ variant_name=str(variant),
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
quantity=2,
diff --git a/tests/test_order.py b/tests/test_order.py
index 79ec2c0dae..7557ba67a9 100644
--- a/tests/test_order.py
+++ b/tests/test_order.py
@@ -68,7 +68,9 @@ def test_add_variant_to_order_adds_line_for_new_variant(
assert line.product_sku == variant.sku
assert line.quantity == 1
assert line.unit_price == TaxedMoney(net=Money(10, "USD"), gross=Money(10, "USD"))
- assert line.translated_product_name == variant.display_product(translated=True)
+ assert line.translated_product_name == str(variant.product.translated)
+ assert line.variant_name == str(variant)
+ assert line.product_name == str(variant.product)
@pytest.mark.parametrize("track_inventory", (True, False))
diff --git a/tests/test_seo_schema.py b/tests/test_seo_schema.py
index 4630e61441..c7ea2b3efa 100644
--- a/tests/test_seo_schema.py
+++ b/tests/test_seo_schema.py
@@ -31,12 +31,12 @@ def test_get_product_data_with_image(order_with_lines, product_with_image):
line = order_with_lines.lines.first()
variant = product_with_image.variants.first()
line.variant = variant
- line.product_name = variant.display_product()
+ line.product_name = str(variant)
line.save()
organization = get_organization()
result = get_product_data(line, organization)
assert "image" in result["itemOffered"]
- assert result["itemOffered"]["name"] == variant.display_product()
+ assert result["itemOffered"]["name"] == str(variant)
def test_get_order_confirmation_markup(order_with_lines):