1

I have need to load an order item's 'purchased entity' object so that I can object the base product's title for an order item.

In my scope, I have the order_item object and, from there I have access to the 'purchased entity' ID number. But I can figure out how to load the 'purchased entity'object from this ID number.

EDIT:

Thanks to Chance G I was able to achieve what I needed by:

  $product_variation = $order_item->getPurchasedEntity();
  $product_id = $product_variation->product_id->getString();
  $product = \Drupal\commerce_product\Entity\Product::load($product_id);
  $product_title = $product->getTitle();
2
  • I didn't realize you need the product title and not the product variation title. I can confirm this later but if you don't need the intermediate variables you can probably do: $order_item->getPurchasedEntity()->getProduct()->getTitle(); – Chance G Feb 13 '18 at 20:01
  • Nice! That snipped did indeed work. Nice and clean. Thanks! – quantumized Feb 13 '18 at 20:10
3

If your $order_item is an instance of the OrderItem class you should be able to do:

$product_variation = $order_item->getPurchasedEntity();
$title = $product_variation->getTitle();

If you have an $order you should be able to do:

foreach ($order->getItems() as $order_item) {
  $product_variation = $order_item->getPurchasedEntity();
  $title = $product_variation->getTitle();
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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