Orders and line items are not multilingual, they exist in only one language at a time.
At the same time, products, discounts, shipping rates are multilingual.

To guard against data changes, the $line_item->title receives the product title when the product is added to cart.
So this means that the line item ends up with a specific translation (FR if the site/product was viewed in french, for example).

Suggestion:
$order->langcode holds the order language. Selected on the order add form, or created by the CartProvider.
This language is used when copying any multilingual data. Once an order is completed in french, the line items stay in french.

Does that make sense? It's potentially weird if the customer views the site in a different language the next time, but the line items stay in the old language, but I don't see a way around that if we want to keep the line item title copying (and we do, cause EU laws require invoices and orders to be unchanged after they are placed).

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

bojanz created an issue. See original summary.

pcambra’s picture

As for a conversation with bojanz on IRC.

Option #1 is having the line item title/label stored as it is using the order language. No further modifications once order moves away from cart statuses.

Option #2 is to provide some sort of tool to extend what a line item label is, probably a formatter that can be overriden by custom code and "stuff" whatever is relevant (attributes, id?) for a given store in that label.

harings_rob’s picture

Assigned: Unassigned » harings_rob
harings_rob’s picture

Status: Active » Needs review
mglaman’s picture

Status: Needs review » Needs work

Kicking back to needs work because PR has conflicts, and not sure what the exact steps forward are going to be.

harings_rob’s picture

Status: Needs work » Needs review

I rerolled onto 8.x-2.x latest codebase.

willeaton’s picture

Hi, I've arrived here because I'm trying to figure out the language of an order to be able to send the confirmation email in the correct language. My issues are:

  • Order has no langcode associated
  • The user in most cases on this site are anonymous so can't even detect their default language
  • While I've managed to override the email template for order confirmation, the translated strings aren't imported because the email is never triggered with a language context. This means that the translations are never imported as interface translations.

For me, a very basic piece of information, that has been available since drupal 5, is now missing. Is there a quick fix to set order language based on interface language on cart creation? At least then I can choose a different template file per language (awful solution but nevertheless a solution for my client) based on the order language.

bojanz’s picture

Just wanted to link to https://www.drupal.org/project/commerce/issues/2981193#comment-12660271, which clarifies our reasoning

willeaton’s picture

Hi @Bojanz, that makes sense for labels, but what about storing the order language for order related events that occur later on? An example is emails triggered by admin events (order status change). You can't use "current language" as the admin language is independent from the user language, and you currently have no idea what the order language is, just labels set in stone. Would it take that much to create an order language field?

bojanz’s picture

@willeaton
The whole point of this issue is to add an order language field.

mglaman’s picture

CaptainPinkey’s picture

Have there been any changes here?

While the system respects the user language, this doesn't account for anonymous orders on an multilang site and emails that are sent on order transitions. (No user to get the lang from and the current lang would be the preferred language of the admin)

So a field representing the active language during the placement of the order would be helpful.

nicothezulu’s picture

Obviously there shall be a better way to solve this,
but if you use the attached patch and create a field at
admin/commerce/config/order-types/default/edit/fields
with the machine name of field_checkout_language it will work.
I suspect a feature like this will be included in future commerce releases,
and needles to say, I use this with the feature together from here: Send order receipt email once an order is placed or validated, depending on the workflow used.
p.s. This will apply for anonymous checkouts
- I did not use DI for the LanguageManager, sorry ;_)

Status: Needs review » Needs work

The last submitted patch, 13: anonymous-checkout-language-mail-D8-2603482-1.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

splash112’s picture

In my use case I would really want to be able to save an order in multiple languages, add a variable to the order with the customer checkout language.

Tom_VDB’s picture

I needed the language the user used to create the order.
I resorted to enabling the langcode field on commerce_order.

In a custom module:

function example_entity_type_build(array &$entity_types) {
  $keys = $entity_types['commerce_order']->getKeys();
  $keys['langcode'] = 'langcode';
  $entity_types['commerce_order']->set('entity_keys', $keys);
}
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Add 'langcode' to 'commerce_order'.
 */
function example_update_8001() {
  $storage_definition = BaseFieldDefinition::create('language')
    ->setLabel(new TranslatableMarkup('Language'))
    ->setDisplayOptions('view', [
      'region' => 'hidden',
    ])
    ->setDisplayOptions('form', [
      'type' => 'language_select',
      'weight' => 2,
    ]);

  \Drupal::entityDefinitionUpdateManager()->installFieldStorageDefinition('langcode', 'commerce_order', 'commerce_order', $storage_definition);
}

This hold the current langcode when the order is first created.
After which it is accessible with $order->langcode()