You are here

public function LineItem::prepareRow in Commerce Migrate 8.2

Same name and namespace in other branches
  1. 3.1.x modules/commerce/src/Plugin/migrate/source/commerce1/LineItem.php \Drupal\commerce_migrate_commerce\Plugin\migrate\source\commerce1\LineItem::prepareRow()
  2. 3.0.x modules/commerce/src/Plugin/migrate/source/commerce1/LineItem.php \Drupal\commerce_migrate_commerce\Plugin\migrate\source\commerce1\LineItem::prepareRow()

Adds additional data to the row.

Parameters

\Drupal\migrate\Row $row: The row object.

Return value

bool FALSE if this row needs to be skipped.

Overrides SourcePluginBase::prepareRow

File

modules/commerce/src/Plugin/migrate/source/commerce1/LineItem.php, line 53

Class

LineItem
Gets Commerce 1 commerce_line_items from source database.

Namespace

Drupal\commerce_migrate_commerce\Plugin\migrate\source\commerce1

Code

public function prepareRow(Row $row) {
  $row
    ->setSourceProperty('data', unserialize($row
    ->getSourceProperty('data')));
  $row
    ->setSourceProperty('title', $row
    ->getSourceProperty('line_item_label'));

  // Get the product title from the commerce_product table.
  if ($row
    ->getSourceProperty('type') === 'product') {
    $label = $row
      ->getSourceProperty('line_item_label');
    $query = $this
      ->select('commerce_product', 'cp')
      ->fields('cp', [
      'title',
    ])
      ->condition('cp.sku', $label);
    $title = $query
      ->execute()
      ->fetchCol();
    $row
      ->setSourceProperty('title', reset($title));
  }

  // Get Field API field values.
  $line_item_id = $row
    ->getSourceProperty('line_item_id');
  $revision_id = $row
    ->getSourceProperty('revision_id');
  foreach (array_keys($this
    ->getFields('commerce_line_item', $row
    ->getSourceProperty('type'))) as $field) {
    $row
      ->setSourceProperty($field, $this
      ->getFieldValues('commerce_line_item', $field, $line_item_id, $revision_id));
  }

  // Include the number of currency fraction digits in all prices.
  $currencyRepository = new CurrencyRepository();
  $prices = [
    'commerce_unit_price',
    'commerce_total',
  ];
  foreach ($prices as $price) {
    $value = $row
      ->getSourceProperty($price);
    if ($value) {
      $currency_code = $value[0]['currency_code'];
      $value[0]['fraction_digits'] = $currencyRepository
        ->get($currency_code)
        ->getFractionDigits();
      $row
        ->setSourceProperty($price, $value);
    }
  }
  $order_id = $row
    ->getSourceProperty('order_id');

  // Get line item counts so the adjustments can be split across lines.
  $query = $this
    ->select('commerce_line_item', 'li')
    ->condition('order_id', $order_id)
    ->condition('type', 'product');
  $query
    ->addExpression('COUNT(line_item_id)', 'num_product_line');
  $query
    ->addExpression('MAX(line_item_id)', 'max_line_item_id');
  $results = $query
    ->execute()
    ->fetchAll();
  $row
    ->setSourceProperty('num_product_line', $results[0]['num_product_line']);
  $row
    ->setSourceProperty('max_line_item_id', $results[0]['max_line_item_id']);

  // Get any shipping line for this order. This is to identify and not
  // migrate shipping price components.
  $query = $this
    ->select('commerce_line_item', 'li')
    ->fields('li')
    ->condition('type', 'shipping')
    ->condition('order_id', $order_id);
  $shipping = $query
    ->execute()
    ->fetchAll();
  $row
    ->setSourceProperty('shipping', $shipping);

  // Get all price components on this order so the discounts can be found
  // and converted to adjustments on the line item.
  $order_id = $row
    ->getSourceProperty('order_id');
  $row
    ->setSourceProperty('order_components', $this
    ->getFieldValues('commerce_order', 'commerce_order_total', $order_id));
  return parent::prepareRow($row);
}