How to programmatically apply a fee/promotion? I am trying to use the new commerce_fee module to programmatically apply a fee during admin payment flow. I did a simple form_alter to the commerce_payment_add form to have a list of my Fee entities show up as checkboxes. This allows staff to add the specific fee if they choose.
I also set:
$form['payment']['amount']['#default_value']['number'] = $form['payment']['amount']['#default_value']['number'] + $form_state-&gt;getValues()['fee_amount'];

so that the fee is added in to the next step as the amount which will be charged to the customer's CC.
But the Fee is not properly added to my Order. I have seen Commerce documentation and a post here re: this but they do not work. At the moment I have a #submit added to the payment_add_form which follows some of the code I have seen; but no luck:
function _sia_add_fee_to_order (&amp;$form, $form_state) {
  // its crazy that i need to figure out the Order from the URL; why isn't it in the form??
  $url = $_SERVER['REQUEST_URI'];
  $bits = explode('/', $url);
  $order_id = $bits[4];

  $order = Order::load($order_id);
  $fee = current(Drupal\commerce_fee\Entity\Fee::loadMultiple());

  $price = $fee-&gt;getPlugin()-&gt;getConfiguration()['amount'];
  $splitter = \Drupal::getContainer()-&gt;get('commerce_order.price_splitter');
  $amounts = $splitter-&gt;split($order, new Price($price['number'], $price['currency_code']));

  foreach ($order-&gt;getItems() as $order_item) {
    $order_item-&gt;addAdjustment(new Adjustment([
      'type' =&gt; 'fee',
      'label' =&gt; $fee-&gt;getName(),
      'amount' =&gt; $amounts[$order_item-&gt;id()],
      'source_id' =&gt; $fee-&gt;id(),
    ]));
  }

  $fee-&gt;apply($order);
  $order-&gt;recalculateTotalPrice();
  $order-&gt;save();
}

The post I saw here left out all the commerce documentation steps of adding adjustments to each line item and simply had the $fee-&gt;apply() function. Neither of theses work.
ADDED:
More info about commerce_fee: https://www.drupal.org/project/commerce/issues/2903716, https://github.com/lisastreeter/commerce_fee
This is basically the commerce &quot;promotion&quot; module but redone to be more specific to fees. The question i have posted could be asked re: promotions as well; as should apply equally to either.
After a lot of trial and error I thought of moving the code out of the form submit function and into an event subscription; in particular, i moved it to the commerce_order.order.paid event.
So I ended up with this in an existing event subscriber call back I already had:
  public function placeOrder($event) {
    $order = $event-&gt;getOrder();

    $fee = current(\Drupal\commerce_fee\Entity\Fee::loadMultiple());
    $price = $fee-&gt;getPlugin()-&gt;getConfiguration()['amount'];
    $splitter = \Drupal::getContainer()-&gt;get('commerce_order.price_splitter');
    $amounts = $splitter-&gt;split($order, new Price($price['number'], $price['currency_code']));
    foreach ($order-&gt;getItems() as $order_item) {
      $adjustments = $order_item-&gt;getAdjustments();
      $adjustments[] = new Adjustment([
        'type' =&gt; 'fee',
        'label' =&gt; $fee-&gt;getName(),
        'amount' =&gt; $amounts[$order_item-&gt;id()],
        'source_id' =&gt; $fee-&gt;id(),
      ]);
      $order_item-&gt;setAdjustments($adjustments);
      $order_item-&gt;save();
    }
  }

This code includes code to split the fee up among all the items; which may or may not be what you want (although doesn't seem like it could ever be a bad idea).