CommerceAdjustments.php in Commerce Migrate 3.1.x
File
src/Plugin/migrate/process/CommerceAdjustments.php
View source
<?php
namespace Drupal\commerce_migrate\Plugin\migrate\process;
use CommerceGuys\Intl\Calculator;
use Drupal\commerce_order\Adjustment;
use Drupal\commerce_price\Price;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
class CommerceAdjustments extends ProcessPluginBase {
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 = [];
$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)));
}
}
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';
$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;
}
}