The site default language is traditional Chinese.

Upon adding a product (in Chinese) of a product type, after clicking Save and publish, a new page with the following error would be prompted: The website encountered an unexpected error. Please try again later.
However, the /admin/commerce/products page showed that the product had been added.

Two errors were logged:
1. Message Warning: Invalid argument supplied for foreach() in Drupal\commerce_product\Entity\Product->postSave() (line 301 of /srv/bindings/615321ab035a455f92f948b22c68d3e1/code/web/modules/contrib/commerce/modules/product/src/Entity/Product.php) #0

2. InvalidArgumentException: Field variations is unknown. in Drupal\Core\Entity\ContentEntityBase->getTranslatedField() (line 471 of /srv/bindings/615321ab035a455f92f948b22c68d3e1/code/web/core/lib/Drupal/Core/Entity/ContentEntityBase.php

Comments

wtchan created an issue. See original summary.

mglaman’s picture

Issue tags: -add product

We have errors surrounding content_translation, this might be related to something in the queue. Any help debugging would be great.

In postSave we ensure variants have a valid backreference.

  /**
   * {@inheritdoc}
   */
  public function postSave(EntityStorageInterface $storage, $update = TRUE) {
    parent::postSave($storage, $update);

    // Ensure there's a back-reference on each product variation.
    foreach ($this->variations as $item) {
      $variation = $item->entity;
      if ($variation->product_id->isEmpty()) {
        $variation->product_id = $this->id();
        $variation->save();
      }
    }
  }

Apparently this seems to explode when translations are enabled.

agoradesign’s picture

hmm, I do have a multi-lingual site up and running. both product entities and variations are translatable. I never experienced this problem. But I think, I have never really created a product via UI because everything is coming via migrate...

mglaman’s picture

Issue tags: +Needs tests

We really need i18n test coverage

bojanz’s picture

Status: Active » Closed (outdated)