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…

  1. 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?

  2. And is there a faster/cheaper way to retrieve the shipping service without loading the entire order -and- all line items?

  3. 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

enter image description here

function MYTHEME_commerce_price_formatted_components($variables) {
  global $user;
  $line_item_title = '';
  $order_number = '';
  $path_parts = explode('/', request_path() );
  if( sizeof($path_parts) == 4 && $path_parts[0] == 'user' && $path_parts[2] == 'orders') {
    $order = commerce_order_load($path_parts[3]);
    // dpm($order);
  }
  if( isset($order) && !$order )
    $order = commerce_cart_order_load($user->uid);
  if( isset($order) && $order) {    
    foreach ($order->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->type->value());
        // for shipping line items
      if ($line_item_wrapper->type->value() == "shipping") {
          // shipping data
        $shipping_data = $line_item_wrapper->value()->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 => $component) {
//    dpm($component);
    if($component)
    $rows[] = array(
      'data' => array(
        array(
          'data' => ($component['title'] == 'Shipping' && $line_item_title ) ? $line_item_title :  $component['title'],
          'class' => array('component-title'),
        ),
        array(
          'data' => $component['formatted_price'],
          'class' => array('component-total'),
        ),
      ),
      'class' => array(drupal_html_class('component-type-' . $name)),
    );
  }
  return theme('table', array('rows' => $rows, 'attributes' => array('class' => array('commerce-price-formatted-components'))));
}
This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

How to modify the commerce price formatter label to match shipping service?

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.