Clarify order language behavior

Created on 28 October 2015, about 9 years ago
Updated 8 June 2024, 6 months ago

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).

📌 Task
Status

Needs work

Version

2.0

Component

User experience

Created by

🇷🇸Serbia bojanz

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇧🇪Belgium Tom_VDB

    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()

  • 🇮🇹Italy trickfun

    I have the same problem but so far no solution.

Production build 0.71.5 2024