Score:1

How to obtain a node id and product type from the product variation id?

gu flag

I have a view in Drupal that has a field called Index product variation search: Item ID. Its description is "the item's internal (search API specific) ID".

Here is an example of what this field looks like: entity:commerce_product_variation/111:en

What is this number (111)--is it the product_variation id?

Basically, I am sending in this number through a POST API call (in javascript). I need to use this number to obtain the node id and the product type for a product?

There is something on How can I retrieve the product ID? but it needs the sku (stock keeping unit) which I don't have.

Score:1
us flag

Load the variation entity through entity type manager dependency injection:

$variation = $this->entityTypeManager->getStorage('commerce_product_variation')->load($variationId);

Or if you are in a hook:

$variation = \Drupal::entityTypeManager()->getStorage('commerce_product_variation')->load($variationId);

Then you can retrieve product entity:

$product = $variation->product_id->entity;

// Product id
$productId = $product->id();
// Product type
$productType = $product->bundle();
Shane avatar
gu flag
One quick question: in the example I gave (entity:commerce_product_variation/111:en), the $variationId would be 111, right? Thanks
TheDrot avatar
us flag
Yes thats right
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.