You are here

public function Order::clearAdjustments in Commerce Core 8.2

Removes all adjustments that belong to the order.

This includes both order and order item adjustments, with the exception of adjustments added via the UI.

Return value

$this

Overrides OrderInterface::clearAdjustments

File

modules/order/src/Entity/Order.php, line 376

Class

Order
Defines the order entity class.

Namespace

Drupal\commerce_order\Entity

Code

public function clearAdjustments() {
  $locked_callback = function ($adjustment) {

    /** @var \Drupal\commerce_order\Adjustment $adjustment */
    return $adjustment
      ->isLocked();
  };

  // Remove all unlocked adjustments.
  foreach ($this
    ->getItems() as $order_item) {

    /** @var \Drupal\commerce_order\Adjustment[] $adjustments */
    $adjustments = array_filter($order_item
      ->getAdjustments(), $locked_callback);

    // Convert legacy locked adjustments.
    if ($adjustments && $order_item
      ->usesLegacyAdjustments()) {
      foreach ($adjustments as $index => $adjustment) {
        $adjustments[$index] = $adjustment
          ->multiply($order_item
          ->getQuantity());
      }
    }
    $order_item
      ->set('uses_legacy_adjustments', FALSE);
    $order_item
      ->setAdjustments($adjustments);
  }
  $adjustments = array_filter($this
    ->getAdjustments(), $locked_callback);
  $this
    ->setAdjustments($adjustments);
  return $this;
}