How to modify the commerce price formatter label to match shipping service? Please see attachment. Using Commerce Kickstart 2.

I have a working solution, but I am hoping someone can tell me how to improve it, as it 'feels' poor.

The footer for the default shopping cart view is a footer called commerce_order_total which shows a subtotal for each line item type... typically labeled 'Subtotal' for products and then a generic 'Shipping' label for shipping line items. 

That generic 'Shipping' label is hated by our customers. They expect to see the actual shipping service (UPS, Fedex, etc.) So I created a work-around, which I show below, but I fear it is not an efficient solution... basically I just made a tweaked copy of the default commerce code for the price formatter.

What I hope to improve...


Surely there must be a more robust way for the footer to be 'aware' of whether the page being viewed is a cart or an order from history without having to look at the URI, right?
And is there a faster/cheaper way to retrieve the shipping service without loading the entire order -and- all line items?
I was thinking there must be a more granular way to override the description of 'Shipping' in that footer without having to re-invent the entire wheel. I just gave up and did the old cut/paste.


TIA,

---JC 



function MYTHEME_commerce_price_formatted_components($variables) {
  global $user;
  $line_item_title = '';
  $order_number = '';
  $path_parts = explode('/', request_path() );
  if( sizeof($path_parts) == 4 &amp;&amp; $path_parts[0] == 'user' &amp;&amp; $path_parts[2] == 'orders') {
    $order = commerce_order_load($path_parts[3]);
    // dpm($order);
  }
  if( isset($order) &amp;&amp; !$order )
    $order = commerce_cart_order_load($user-&gt;uid);
  if( isset($order) &amp;&amp; $order) {    
    foreach ($order-&gt;commerce_line_items['und'] as $line) {
      $line_item = commerce_line_item_load($line['line_item_id']);
      $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
      // line item TYPE display title
      $line_item_type_title = commerce_line_item_type_get_name($line_item_wrapper-&gt;type-&gt;value());
        // for shipping line items
      if ($line_item_wrapper-&gt;type-&gt;value() == "shipping") {
          // shipping data
        $shipping_data = $line_item_wrapper-&gt;value()-&gt;data;
          // shipping method display title
        $line_item_title = $shipping_data['shipping_service']['title'];
      }     
    }
  }
  // Add the CSS styling to the table.
  drupal_add_css(drupal_get_path('module', 'commerce_price') . '/theme/commerce_price.theme.css');
  // Build table rows out of the components.
  $rows = array();
//  dpm($variables['components']);
  foreach ($variables['components'] as $name =&gt; $component) {
//    dpm($component);
    if($component)
    $rows[] = array(
      'data' =&gt; array(
        array(
          'data' =&gt; ($component['title'] == 'Shipping' &amp;&amp; $line_item_title ) ? $line_item_title :  $component['title'],
          'class' =&gt; array('component-title'),
        ),
        array(
          'data' =&gt; $component['formatted_price'],
          'class' =&gt; array('component-total'),
        ),
      ),
      'class' =&gt; array(drupal_html_class('component-type-' . $name)),
    );
  }
  return theme('table', array('rows' =&gt; $rows, 'attributes' =&gt; array('class' =&gt; array('commerce-price-formatted-components'))));
}
I think this is equivalent - not sure there's a better way than loading all the line items. 

function HOOK_commerce_price_formatted_components_alter(&amp;$components, $item, $entity) {
  foreach ($entity-&gt;commerce_line_items['und'] as $line) {
    $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line['line_item_id']);

    if ($line_item_wrapper-&gt;type-&gt;value() == "shipping") {
      $shipping_data = $line_item_wrapper-&gt;value()-&gt;data;
      $service_title = $shipping_data['shipping_service']['title'];
      break;
    }
  }

  if (isset($service_title)) {
    $components['shipping']['title'] = $service_title;
  }
}


(The title is coming from the price component type - maybe one could use hook_commerce_shipping_service_info_alter() to change the price_component to something per shipping service, and set the title in hook_commerce_price_component_type_info(), but that probably isn't any neater.)