You are here

class CommerceAdjustments in Commerce Migrate 3.1.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/migrate/process/CommerceAdjustments.php \Drupal\commerce_migrate\Plugin\migrate\process\CommerceAdjustments
  2. 3.0.x src/Plugin/migrate/process/CommerceAdjustments.php \Drupal\commerce_migrate\Plugin\migrate\process\CommerceAdjustments

Converts a nested array into Commerce Adjustments.

The commerce adjustments process plugin converts adjustment data to Commerce Adjustments. The input value is an indexed array of adjustment data arrays.

Example:


process:
 field_adjustment:
   plugin: commerce_adjustments
   source: input_array

process:
 field_adjustment:
   plugin: commerce_adjustments
   source:
      - input_array_A
      - input_array_B

Plugin annotation


@MigrateProcessPlugin(
  id = "commerce_adjustments",
  handle_multiples = true
)

Hierarchy

Expanded class hierarchy of CommerceAdjustments

1 file declares its use of CommerceAdjustments
CommerceAdjustmentsTest.php in tests/src/Kernel/Plugin/migrate/process/CommerceAdjustmentsTest.php

File

src/Plugin/migrate/process/CommerceAdjustments.php, line 43

Namespace

Drupal\commerce_migrate\Plugin\migrate\process
View source
class CommerceAdjustments extends ProcessPluginBase {

  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    if (is_array($value) && !empty($value)) {
      $adjustments = [];
      $i = 0;
      foreach ($value as $data) {
        if ($data) {
          $adjustment = [];

          // Thrown an exception if amount and currency_code are not set. Let
          // Price validate the amount.
          $not_set = [];
          foreach ([
            'amount',
            'currency_code',
          ] as $property) {
            if (!isset($data[$property])) {
              $not_set[] = $property;
            }
          }
          if ($not_set) {
            if (count($not_set) > 1) {
              throw new MigrateSkipRowException(sprintf("Properties 'amount' and 'currency_code' are not set for adjustment '%s'", var_export($data, TRUE)));
            }
            else {
              throw new MigrateSkipRowException(sprintf("Property '%s' is not set for adjustment '%s'", reset($not_set), var_export($data, TRUE)));
            }
          }

          // Skip the row if the price can't be created.
          try {
            $adjustment['amount'] = new Price(Calculator::trim($data['amount']), $data['currency_code']);
          } catch (\InvalidArgumentException $e) {
            throw new MigrateSkipRowException(sprintf('Failed creating price for adjustment %s', var_export($data, TRUE)));
          }
          $adjustment['delta'] = $i++;
          $adjustment['type'] = !empty($data['type']) ? $data['type'] : 'custom';

          // Allow the label to be in a title or label property.
          $adjustment['label'] = !empty($data['label']) ? $data['label'] : '';
          if (empty($adjustment['label'])) {
            $adjustment['label'] = !empty($data['title']) ? $data['title'] : 'Custom';
          }
          $adjustment['percentage'] = !empty($data['percentage']) ? $data['percentage'] : NULL;
          $adjustment['source_id'] = !empty($data['source_id']) ? $data['source_id'] : 'custom';
          $adjustment['included'] = !empty($data['included']) ? $data['included'] : FALSE;
          $adjustment['locked'] = !empty($data['locked']) ? $data['locked'] : TRUE;
          $adjustments[] = new Adjustment($adjustment);
        }
      }
      return $adjustments;
    }
    return $value;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommerceAdjustments::transform public function Performs the associated process. Overrides ProcessPluginBase::transform
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 98
ProcessPluginBase::multiple public function Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface::multiple 3
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.