drupaldrupal-modules

Drupal Commerce: Can't get variation type machine name from product variation object


I successfully receive product variation object by its id

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

Then I successfully receive its structure like

(
    [variation_id] => Array
        (
            [x-default] => 8
    )

    [type] => Array
        (
            [x-default] => router
        )

    [uuid] => Array
        (
            [x-default] => a44c2c31-2131-4c99-82a6-856b566d97cf
        )
    ...

by print_r() like

echo '<pre>';
print_r($variation);
echo '</pre>';

Now if I try to get SKU through $variation->sku->value, I get it

But if I try to get variation type machine name through $variation->type->value I get nothing (and gettype($variation->type->value) returns NULL).

Still, we see it in the structure with the value router

Why and how to get the machine name ?


Solution

  • It's strange: the print_r() shows the way is $variation->type->value

    But I've just successfully got it by

    $variation->type[0]->target_id