You are here

public function AdjustmentTransformer::combineAdjustments in Commerce Core 8.2

Combines adjustments with the same type and source ID.

For example, all tax adjustments generated by the same tax type will be combined into a single adjustment, which can then be shown in total summaries.

Parameters

\Drupal\commerce_order\Adjustment[] $adjustments: The adjustments.

Return value

\Drupal\commerce_order\Adjustment[] The combined adjustments.

Overrides AdjustmentTransformerInterface::combineAdjustments

1 call to AdjustmentTransformer::combineAdjustments()
AdjustmentTransformer::processAdjustments in modules/order/src/AdjustmentTransformer.php
Combines, sorts, and rounds the given adjustments.

File

modules/order/src/AdjustmentTransformer.php, line 51

Class

AdjustmentTransformer

Namespace

Drupal\commerce_order

Code

public function combineAdjustments(array $adjustments) {
  $combined_adjustments = [];
  foreach ($adjustments as $index => $adjustment) {
    $type = $adjustment
      ->getType();
    $source_id = $adjustment
      ->getSourceId();
    if (empty($source_id)) {

      // Adjustments without a source ID are always shown standalone.
      $key = $index;
    }
    else {

      // Adjustments with the same type and source ID are combined.
      $key = $type . '_' . $source_id;
    }
    if (empty($combined_adjustments[$key])) {
      $combined_adjustments[$key] = $adjustment;
    }
    else {
      $combined_adjustments[$key] = $combined_adjustments[$key]
        ->add($adjustment);
    }
  }

  // The keys used for combining are irrelevant to the caller.
  $combined_adjustments = array_values($combined_adjustments);
  return $combined_adjustments;
}