You are here

protected function OrderItemDiscountAdjustment::getAdjustment in Commerce Migrate 3.1.x

Same name and namespace in other branches
  1. 8.2 modules/commerce/src/Plugin/migrate/process/commerce1/OrderItemDiscountAdjustment.php \Drupal\commerce_migrate_commerce\Plugin\migrate\process\commerce1\OrderItemDiscountAdjustment::getAdjustment()
  2. 3.0.x modules/commerce/src/Plugin/migrate/process/commerce1/OrderItemDiscountAdjustment.php \Drupal\commerce_migrate_commerce\Plugin\migrate\process\commerce1\OrderItemDiscountAdjustment::getAdjustment()

Get the adjustments on this order item.

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.

Return value

array An array of adjustment data.

Throws

\Drupal\migrate\MigrateSkipRowException

2 calls to OrderItemDiscountAdjustment::getAdjustment()
OrderItemDiscountAdjustment::transform in modules/commerce/src/Plugin/migrate/process/commerce1/OrderItemDiscountAdjustment.php
Performs the associated process.
TestOrderItemDiscountAdjustment::getAdjustment in modules/commerce/tests/src/Unit/Plugin/migrate/process/commerce1/OrderItemDiscountAdjustmentTest.php
Get the adjustments on this order item.
1 method overrides OrderItemDiscountAdjustment::getAdjustment()
TestOrderItemDiscountAdjustment::getAdjustment in modules/commerce/tests/src/Unit/Plugin/migrate/process/commerce1/OrderItemDiscountAdjustmentTest.php
Get the adjustments on this order item.

File

modules/commerce/src/Plugin/migrate/process/commerce1/OrderItemDiscountAdjustment.php, line 161

Class

OrderItemDiscountAdjustment
Builds an array of adjustment data.

Namespace

Drupal\commerce_migrate_commerce\Plugin\migrate\process\commerce1

Code

protected function getAdjustment($value, MigrateExecutableInterface $migrate_executable, Row $row) {
  $adjustment = [];
  if (is_array($value)) {
    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);
        $price = new Price((string) $price_scaled['number'], $price_scaled['currency_code']);
        $price = $this->rounder
          ->round($price);
        $num_product_line = $row
          ->getSourceProperty('num_product_line');
        $last_line = FALSE;
        if ($row
          ->getSourceProperty('line_item_id') == $row
          ->getSourceProperty('max_line_item_id')) {
          $last_line = TRUE;
        }
        $amount = $this
          ->split($num_product_line, $last_line, $price);
        if ($amount) {
          $adjustment = [
            'type' => $type,
            'label' => $label,
            'amount' => $amount
              ->getNumber(),
            'currency_code' => $amount
              ->getCurrencyCode(),
            'percentage' => $percentage,
            'source_id' => 'custom',
            'included' => FALSE,
            'locked' => TRUE,
          ];
        }
      }
    }
  }
  return $adjustment;
}