You are here

public function OrderItemAdjustment::transform in Commerce Migrate 3.1.x

Same name in this branch
  1. 3.1.x modules/ubercart/src/Plugin/migrate/process/OrderItemAdjustment.php \Drupal\commerce_migrate_ubercart\Plugin\migrate\process\OrderItemAdjustment::transform()
  2. 3.1.x modules/commerce/src/Plugin/migrate/process/commerce1/OrderItemAdjustment.php \Drupal\commerce_migrate_commerce\Plugin\migrate\process\commerce1\OrderItemAdjustment::transform()
Same name and namespace in other branches
  1. 8.2 modules/commerce/src/Plugin/migrate/process/commerce1/OrderItemAdjustment.php \Drupal\commerce_migrate_commerce\Plugin\migrate\process\commerce1\OrderItemAdjustment::transform()
  2. 3.0.x modules/commerce/src/Plugin/migrate/process/commerce1/OrderItemAdjustment.php \Drupal\commerce_migrate_commerce\Plugin\migrate\process\commerce1\OrderItemAdjustment::transform()

Performs the associated process.

Parameters

mixed $value: The value to be transformed.

\Drupal\migrate\MigrateExecutableInterface $migrate_executable: The migration in which this process is being executed.

\Drupal\migrate\Row $row: The row from the source to process. Normally, just transforming the value is adequate but very rarely you might need to change two columns at the same time or something like that.

string $destination_property: The destination property currently worked on. This is only used together with the $row above.

Return value

string|array The newly transformed value.

Overrides CommercePrice::transform

File

modules/commerce/src/Plugin/migrate/process/commerce1/OrderItemAdjustment.php, line 36

Class

OrderItemAdjustment
Builds an array of adjustment data.

Namespace

Drupal\commerce_migrate_commerce\Plugin\migrate\process\commerce1

Code

public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
  $adjustment = [];
  if (is_array($value)) {
    if (empty($value['name'])) {
      throw new MigrateSkipRowException(sprintf("Adjustment has no name for line item '%s'.", $row
        ->getSourceProperty('line_item_id')));
    }
    if ($value['name'] !== 'base_price') {
      $parts = explode('|', $value['name'], -1);
      if (!empty($parts)) {
        $percentage = NULL;
        $type = '';
        $label = '';
        $amount = (string) $value['price']['amount'];
        $currency_code = $value['price']['currency_code'];
        if ($parts[0] === 'tax') {
          $type = 'tax';
          $tax_rate = $value['price']['data']['tax_rate'];
          $label = $tax_rate['display_title'];
          $percentage = $tax_rate['rate'];
        }
        if ($parts[0] === 'discount') {
          $type = 'promotion';
          $label = $value['price']['data']['discount_component_title'];
        }
        if (empty($type)) {
          throw new MigrateSkipRowException(sprintf("Unknown adjustment type for line item '%s'.", $row
            ->getSourceProperty('line_item_id')));
        }

        // Scale the incoming price by the fraction digits.
        $fraction_digits = isset($value['price']['fraction_digits']) ? $value['price']['fraction_digits']['fraction_digits'] : '2';
        $input = [
          'amount' => $amount,
          'fraction_digits' => $fraction_digits,
          'currency_code' => $currency_code,
        ];
        $price_scaled = parent::transform($input, $migrate_executable, $row, NULL);
        $adjustment = [
          'type' => $type,
          'label' => $label,
          'amount' => $price_scaled['number'],
          'currency_code' => $currency_code,
          'percentage' => $percentage,
          'source_id' => 'custom',
          'included' => FALSE,
          'locked' => TRUE,
        ];
      }
    }
  }
  return $adjustment;
}