0

How to get product name by id? I want send mail with product name to customer after confirm commerce order.

2 Answers 2

3

A product is an entity, so you can use entity_load().

$product = entity_load('commerce_product', $product_id);
$title = $product->title;

Commerce provides its own shortcut, though:

$product = commerce_product_load($product_id);
$title = $product->title;
3

The entity_load() is deprecated since 8.0.0. For Drupal 9 use:

// Several product ids to load.
$product_id_to_load = [34, 342, 567];

if (count($product_id_to_load) > 0) {
    $entity_manager = \Drupal::entityTypeManager();
    $products = $entity_manager->getStorage('commerce_product')->loadMultiple($product_id_to_load);
    foreach ($products as $product) {
        $titles[$product->product_id->value] = $product->get('title')->value;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.