You are here

public function InvoiceItem::populateFromOrderItem in Commerce Invoice 8.2

Populates the invoice item with field values from the order item.

Parameters

\Drupal\commerce_Order\Entity\OrderItemInterface $order_item: The order item.

Return value

$this

Overrides InvoiceItemInterface::populateFromOrderItem

File

src/Entity/InvoiceItem.php, line 316

Class

InvoiceItem
Defines the invoice item entity class.

Namespace

Drupal\commerce_invoice\Entity

Code

public function populateFromOrderItem(OrderItemInterface $order_item) {
  $purchased_entity = $order_item
    ->getPurchasedEntity();
  if ($purchased_entity) {
    $langcode = $this
      ->language()
      ->getId();
    if ($purchased_entity
      ->hasTranslation($langcode)) {
      $purchased_entity = $purchased_entity
        ->getTranslation($langcode);
    }
    $title = $purchased_entity
      ->getOrderItemTitle();
  }
  else {
    $title = $order_item
      ->getTitle();
  }
  $this
    ->set('title', $title);

  // If this is not the default translation, there's no need to reset
  // the untranslatable fields.
  if ($this
    ->isDefaultTranslation()) {
    $this
      ->set('adjustments', $order_item
      ->getAdjustments());
    $this
      ->set('quantity', $order_item
      ->getQuantity());
    $this
      ->set('unit_price', $order_item
      ->getUnitPrice());
    $this->order_item_id = $order_item
      ->id();
  }
}