Skip to content

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

Add Translateable product name to OrderLine

Pacu2129c02d9c2b9
diff --git a/saleor/order/models.py b/saleor/order/models.py
index a64a593800..b2111420c6 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -186,6 +186,7 @@ class OrderLine(models.Model):
blank=True, null=True)
# max_length is as produced by ProductVariant's display_product method
product_name = models.CharField(max_length=386)
+ translated_product_name = models.CharField(max_length=386, default='')
product_sku = models.CharField(max_length=32)
is_shipping_required = models.BooleanField()
quantity = models.IntegerField(
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index f67252ed00..b90efa97c5 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -160,8 +160,13 @@ def add_variant_to_order(order, variant, quantity, discounts=None, taxes=None):
line.quantity += quantity
line.save(update_fields=['quantity'])
except OrderLine.DoesNotExist:
+ product_name = variant.display_product()
+ translated_product_name = variant.display_product(translated=True)
+ if translated_product_name == product_name:
+ translated_product_name = ''
order.lines.create(
- product_name=variant.display_product(),
+ product_name=product_name,
+ translated_product_name=translated_product_name,
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
quantity=quantity,
diff --git a/saleor/product/models.py b/saleor/product/models.py
index 364f8676e8..e5e68e4281 100644
--- a/saleor/product/models.py
+++ b/saleor/product/models.py
@@ -250,11 +250,16 @@ class ProductVariant(models.Model):
def is_in_stock(self):
return self.quantity_available > 0
- def display_product(self):
- variant_display = str(self.translated)
+ def display_product(self, translated=False):
+ if translated:
+ product = self.product.translated
+ variant_display = str(self.translated)
+ else:
+ variant_display = str(self)
+ product = self.product
product_display = (
- '%s (%s)' % (self.product.translated, variant_display)
- if variant_display else str(self.product))
+ '%s (%s)' % (product, variant_display)
+ if variant_display else str(product))
return smart_text(product_display)
def get_first_image(self):
diff --git a/saleor/seo/schema/email.py b/saleor/seo/schema/email.py
index ed82aeeeb0..87e1b1cbdd 100644
--- a/saleor/seo/schema/email.py
+++ b/saleor/seo/schema/email.py
@@ -17,7 +17,7 @@ def get_product_data(line, organization):
'@type': 'Offer',
'itemOffered': {
'@type': 'Product',
- 'name': line.product_name,
+ 'name': line.translated_product_name or line.product_name,
'sku': line.product_sku,
},
'price': gross_product_price.amount,
diff --git a/templates/order/_ordered_items_table.html b/templates/order/_ordered_items_table.html
index e54476bdd4..148ced4f01 100644
--- a/templates/order/_ordered_items_table.html
+++ b/templates/order/_ordered_items_table.html
@@ -54,7 +54,7 @@
data-srcset="{% product_first_image line.variant.product size="60x60" method="crop" %} 1x, {% product_first_image line.variant.product size="120x120" method="crop" %} 2x"
class="float-left lazyload lazypreload"
src="{% placeholder size=60 %}">
- <span class="order-details__product__description">{{ line.product_name }}</span>
+ <span class="order-details__product__description">{{ line.translated_product_name|default:line.product_name }}</span>
</a>
</div>
<div class="col-md-4">
@@ -180,7 +180,7 @@
<div class="row">
<div class="col-md-6 col-9">
<a class="link--clean" href="{% if line.variant.product %}{{ line.variant.product.get_absolute_url }}{% endif %}">
- <span class="order-details__product__description">{{ line.order_line.product_name }}</span>
+ <span class="order-details__product__description">{{ line.order_line.translated_product_name|default:line.order_line.product_name }}</span>
</a>
</div>
<div class="col-md-6 col-3">
diff --git a/templates/templated_email/source/partials/_fulfillment_lines.mjml b/templates/templated_email/source/partials/_fulfillment_lines.mjml
index 5895c33ac7..33ee278704 100644
--- a/templates/templated_email/source/partials/_fulfillment_lines.mjml
+++ b/templates/templated_email/source/partials/_fulfillment_lines.mjml
@@ -10,7 +10,7 @@
<tbody>
{% for line in fulfillment %}
<tr class="table-item-row">
- <td>{{ line.order_line.product_name }}</td>
+ <td>{{ line.order_line.translated_product_name|default:line.order_line.product_name }}</td>
<td style="text-align: right;">{{ line.quantity }}</td>
</tr>
{% endfor %}
diff --git a/templates/templated_email/source/partials/_order_lines.mjml b/templates/templated_email/source/partials/_order_lines.mjml
index bec2c24750..ef72496429 100644
--- a/templates/templated_email/source/partials/_order_lines.mjml
+++ b/templates/templated_email/source/partials/_order_lines.mjml
@@ -64,7 +64,7 @@
<tbody>
{% for line in order %}
<tr class="table-item-row">
- <td>{{ line.product_name }}</td>
+ <td>{{ line.translated_product_name|default:line.product_name }}</td>
<td style="text-align: right;">{{ line.quantity }}</td>
<td style="text-align: right;">
{% price line.unit_price display_gross=order.display_gross_prices html=False %}
diff --git a/tests/conftest.py b/tests/conftest.py
index dbf6e57878..d52752b2c6 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -32,7 +32,7 @@ from saleor.page.models import Page
from saleor.product.models import (
AttributeChoiceValue, Category,
Collection, Product, ProductAttribute, ProductAttributeTranslation,
- ProductImage, ProductType, ProductVariant)
+ ProductImage, ProductTranslation, ProductType, ProductVariant)
from saleor.shipping.models import ShippingMethod, ShippingMethodCountry
from saleor.site.models import AuthorizationKey, SiteSettings
@@ -703,7 +703,7 @@ def vatlayer(db, settings, tax_rates, taxes):
@pytest.fixture
-def translated_variant(product):
+def translated_variant_fr(product):
attribute = product.product_type.variant_attributes.first()
return ProductAttributeTranslation.objects.create(
language_code='fr', product_attribute=attribute,
@@ -722,3 +722,10 @@ def translated_product_attribute(product):
def voucher_translation_fr(voucher):
return VoucherTranslation.objects.create(
language_code='fr', voucher=voucher, name='French name')
+
+
+def product_translation_fr(product):
+ return ProductTranslation.objects.create(
+ language_code='fr', product=product, name='French name',
+ description='French description')
diff --git a/tests/test_order.py b/tests/test_order.py
index cea2cf6be6..96d9bed567 100644
--- a/tests/test_order.py
+++ b/tests/test_order.py
@@ -64,11 +64,11 @@ def test_get_tax_rate_by_name_empty_taxes(product):
def test_add_variant_to_order_adds_line_for_new_variant(
- order_with_lines, product, taxes):
+ order_with_lines, product, taxes, product_translation_fr, settings):
order = order_with_lines
variant = product.variants.get()
lines_before = order.lines.count()
-
+ settings.LANGUAGE_CODE = 'fr'
add_variant_to_order(order, variant, 1, taxes=taxes)
line = order.lines.last()
@@ -78,6 +78,8 @@ def test_add_variant_to_order_adds_line_for_new_variant(
assert line.unit_price == TaxedMoney(
net=Money('8.13', 'USD'), gross=Money(10, 'USD'))
assert line.tax_rate == taxes[product.tax_rate]['value']
+ assert line.translated_product_name == variant.display_product(
+ translated=True)
@pytest.mark.parametrize('track_inventory', (True, False))
diff --git a/tests/test_product.py b/tests/test_product.py
index ca948ce01e..66c637a8fd 100644
--- a/tests/test_product.py
+++ b/tests/test_product.py
@@ -634,11 +634,11 @@ def test_product_json_deserialization(default_category, product_type):
def test_variant_picker_data_with_translations(
- product, translated_variant, settings):
+ product, translated_variant_fr, settings):
settings.LANGUAGE_CODE = 'fr'
variant_picker_data = get_variant_picker_data(product)
attribute = variant_picker_data['variantAttributes'][0]
- assert attribute['name'] == translated_variant.name
+ assert attribute['name'] == translated_variant_fr.name
def test_get_product_attributes_data_translation(
diff --git a/tests/test_translation.py b/tests/test_translation.py
index 633ac7f8f2..c3aebd88df 100644
--- a/tests/test_translation.py
+++ b/tests/test_translation.py
@@ -1,19 +1,11 @@
import pytest
-from saleor.discount.models import VoucherTranslation
from saleor.product.models import (
AttributeChoiceValueTranslation, CategoryTranslation,
CollectionTranslation, ProductAttributeTranslation, ProductTranslation,
ProductVariantTranslation)
-def product_translation_fr(product):
- return ProductTranslation.objects.create(
- language_code='fr', product=product, name='French name',
- description='French description')
-
-
@pytest.fixture
def product_translation_pl(product):
return ProductTranslation.objects.create(