You are here

public function OrderAdjustmentShipping::transform in Commerce Migrate 3.0.x

Same name and namespace in other branches
  1. 8.2 modules/commerce/src/Plugin/migrate/process/commerce1/OrderAdjustmentShipping.php \Drupal\commerce_migrate_commerce\Plugin\migrate\process\commerce1\OrderAdjustmentShipping::transform()
  2. 3.1.x modules/commerce/src/Plugin/migrate/process/commerce1/OrderAdjustmentShipping.php \Drupal\commerce_migrate_commerce\Plugin\migrate\process\commerce1\OrderAdjustmentShipping::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 ProcessPluginBase::transform

File

modules/commerce/src/Plugin/migrate/process/commerce1/OrderAdjustmentShipping.php, line 22

Class

OrderAdjustmentShipping
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 = [];

  // It is not an error if $value is not an array. In that case return an
  // empty array.
  if (is_array($value)) {
    if (!isset($value['commerce_total'])) {
      throw new MigrateSkipRowException(sprintf("Adjustment does not have a total for destination '%s'", $destination_property));
    }
    $total = $value['commerce_total'][0];
    if (!isset($total['amount'])) {
      throw new MigrateSkipRowException(sprintf("Adjustment total amount does not exist for destination '%s'", $destination_property));
    }
    if (!isset($total['currency_code'])) {
      throw new MigrateSkipRowException(sprintf("Adjustment currency code does not exist for destination '%s'", $destination_property));
    }
    $fraction_digits = isset($total['fraction_digits']) ? $total['fraction_digits'] : '2';

    // Scale the incoming price by the fraction digits.
    $input = [
      'amount' => $total['amount'],
      'fraction_digits' => $fraction_digits,
      'currency_code' => $total['currency_code'],
    ];
    $price = new CommercePrice([], 'price', '');
    $price_scaled = $price
      ->transform($input, $migrate_executable, $row, NULL);

    // Build the adjustment array.
    $adjustment = [
      'type' => 'shipping',
      'label' => isset($value['line_item_label']) ? $value['line_item_label'] : 'Shipping',
      'amount' => $price_scaled['number'],
      'currency_code' => $price_scaled['currency_code'],
      'sourceId' => 'custom',
      'included' => FALSE,
      'locked' => TRUE,
    ];
  }
  return $adjustment;
}