Sort the products by SKU when seeing order details I have an Ubercart commerce website in Drupal 7.

I'm trying to sort the products listed inside a given completed order by the SKU value.

This is the link: /admin/store/orders/1 or /admin/store/orders/300

I tried to edit uc-order--admin.tpl.php but it did not work. I tried everything but nothing works. Maybe I'm doing it wrong.

I'm new to theming and templating so the code I wrote or the place I did might have been wrong also.

Any ideas on how to sort the products by the values in the SKU field in an order that was already placed?

Inside uc-order--admin.tpl.php I added the following code:

$order_output = array();
$order_info = new stdClass();
$order_id = '';

$array_key_modifier = 0;
foreach ($products as $product) {
    // organize products by sku 
    // make an array with stuff I need
    // sku => array(item,model,price,quantity,subtotal)
    // then sort keys by alphabetical value, output to table
    $array_key = '';
    if (!isset($order_output[$product->model])) {
        $array_key = $product->model;
    } else {
        $array_key = $product->model . $array_key_modifier;
        $array_key_modifier++;
    }

    $order_output[$array_key] = array("title" => $product->title, "model" => $product->model,
        "price" => $product->price, "quantity" => $product->qty, "subtotal" => $product->total_price);
    //  "price");
    $order_info = $product->order; // each product has complete built in order info. Maintain a reference to it
    $order_id = $product->order_id;

    if (!empty($product->data['attributes'])):
        $order_output[$array_key]["attributes"] = $product->data['attributes'];
    endif;
}

ksort($order_output);
uc-order--admin.tpl.php is the "default admin notification template". In other words, the email sent to admins to notify them about a new order. 

You can change the order of the products listed in the order view page by implementing hook_tapir_table_alter()

I have not used this code in production, and do not know what effect it will have on other pages/processes, so test it thoroughly:

function YOUR_MODULE_NAME_tapir_table_alter(&amp;$table, $table_id) {
  if ($table_id == 'uc_op_products_view_table') {
    $sorted_rows = array();
    foreach ($table['#rows'] as $key =&gt; $row) {
      if (!empty($row['#entity']-&gt;model)) {
        $sku = $row['#entity']-&gt;model;
        $new_key = $sku . '_' . $key;
      }
      else {
        $new_key = $key;
      }
      $sorted_rows[$new_key] = $row;
    }
    if ($sorted_rows &amp;&amp; ksort($sorted_rows)) {
      $table['#rows'] = $sorted_rows;
    }
  }
}