How to alter commerce summary total (footer of commerce summary view)

To alter main content view of commerce summary you need to change results part of view, you can do this in different ways, one of it could be hook_preprocess_views_view or with some of the hooks views api offers.
To change order totals in footer, you need to use hook_preprocess_commerce_order_total_summary

So for example, let say you want to add some new variable to be outputted in cart_summary template, you would do this

 * Implements theme_preprocess_commerce_order_total_summary(().
function my_theme_preprocess_commerce_order_total_summary(&$variables){

  /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
  $order = \Drupal::routeMatch()->getParameter('commerce_order');
  if (!empty($order) && $order->getData("special_flag")){
    foreach ($order->getItems() as $item) {

      if ($title = $item->get("type")->getString() == "special_item"){
        $price = $item->getUnitPrice();
        $variables['totals']["special"]  = new Price($price->getNumber(), $price->getCurrencyCode());


We are getting order object with upcasting from magical Drupal::routeMatch()->getParameter (this is somewhat similar to  menu_get_object() in d7) or using arg() object to get values from path and then loading entities.

When adding extra order_items, we did set some special flag like this

$order->setData('special_flag', TRUE);

so that is the IF we are using, as we are checking do we have this data in the first place, and if so then we loop and create new variable (we could do this without this flag also). Finally we set this new variable as new Price object.

To use this variable we need to change commerce-order-total-summary.html.twig so we go there (if you didnt copy it to your theme, you should do that) and add this

{% if totals.special %}
<span class="order-total-line-value">{{ totals.special|commerce_price_format }}</span>
{% endif %}

so we will have this line added to output once it is present and with that commerce order total lines in summary view will change.