Hi,

I like to sort the order view on the weight of a line item.
Now when I change the weight of a line item at in the order edit screen, the weight of the line item is properly saved but it doesn't affect the sorting of the line items at the order view screen.

When I edit the view of the order view screen, I cannot add the weight field as a sorting field.

Is it possible to sort a view on the actual weight of a line item?

Thanks!

CommentFileSizeAuthor
order_view.png25.31 KBmaxplus
order_edit.png29.6 KBmaxplus
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

maxplus’s picture

Title: Order edit: line item weight » Order edit: set line item weight => Order view: sort on line item weight?
maxplus’s picture

I have managed to sort using this technique: https://www.drupal.org/node/2332391 but inside the Commerce line items view, I cannot manage to solve the duplicate results issue after adding the relationship to commerce order (to get the delta field to sort on)

vincentdemers’s picture

The easiest way that I have found to accomplish this is by implementing hook_views_query_alter in a custom module.

function mymodule_views_query_alter(&$view, &$query) {
  if($view->name=='commerce_line_item_table'){ 
    $join = new views_join();
    $join->table = 'field_data_commerce_line_items';
    $join->left_table = 'commerce_line_item';
    $join->field = 'commerce_line_items_line_item_id';
    $join->left_field = 'line_item_id';
    $join->type = 'LEFT';
    
    // Do the actual join
    $query->table_queue['delta'] = array (
      'alias' => 'line_item_delta',
      'table' => 'field_data_commerce_line_items',
      'relationship' => 'commerce_line_item',
      'join' => $join,
    ); 

    $query->orderby[0]['field'] = 'delta';
    $query->orderby[0]['direction'] = 'ASC';  
  }
}