0

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);
  • 1
    the code I wrote or the place I did might have been wrong – What code at what place? Please edit that into your question. Many thanks – leymannx Dec 17 '19 at 20:51
  • see the updated question – Nathan Dec 17 '19 at 20:57
1

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(&$table, $table_id) {
  if ($table_id == 'uc_op_products_view_table') {
    $sorted_rows = array();
    foreach ($table['#rows'] as $key => $row) {
      if (!empty($row['#entity']->model)) {
        $sku = $row['#entity']->model;
        $new_key = $sku . '_' . $key;
      }
      else {
        $new_key = $key;
      }
      $sorted_rows[$new_key] = $row;
    }
    if ($sorted_rows && ksort($sorted_rows)) {
      $table['#rows'] = $sorted_rows;
    }
  }
}
| improve this answer | |
  • Thanks @runswith.... That was exactly what i needed... Tested and everything looks good... You're the man! – Nathan Dec 19 '19 at 15:58
  • You're welcome, glad I could help. – runswithscissors Dec 19 '19 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.