Score:0

My product variation is not shown on the entity form

kn flag

I have many products coming from a migration. This is working for most of the products, but for some of them, the /commerce/modules/product/src/Plugin/Field/FieldWidget/SingleVariationWidget.php widget is empty.

First, I thought that the product didn't have any variation. Turns out, it does.

After some digging, I've found that the formElement function inside SingleVariationWidget isn't retrieving only the variation product.

This variable is getting first an instance of my product, then my product variation.

$variation = !$items->isEmpty() ? $items->first()->entity : NULL;

To bypass this, I've added this patch, but it's a patch, and I still don't know why it happened.

$variation = !$items->isEmpty() ? $items->first()->entity : NULL;

if (!$variation) {
  $param = \Drupal::routeMatch()->getParameter('commerce_product');
  $variation = $this->entityTypeManager->getStorage('commerce_product_variation')->create([
    'type' => reset($this->getFieldSetting('handler_settings')['target_bundles']),
    'langcode' => $items->getEntity()->language()->getId(),
  ]);
  if ($param) {
    $idProduct = $param->id();
    $product = Product::load($idProduct);
    if ($product->getDefaultVariation()) {
      $variation = $product->getDefaultVariation();
    }
  }
}
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.