Commerce orders view for user and his subusers The task is to create a view which will contain all orders of user and his subusers. I use the Commerce Kickstart and Subuser module. I have default view called "My orders" which prints all orders of the logged in user.  I don't know, how to integrate the subuser relationship properly. How can I do it?

Added: this is the sample. it works only for one user. I need it to be worked for user and his subusers 

The result should be the following: the one table like at the screenshot but with different (specified) users - the current user and his subusers...
Orders are entities. Views 3 on Drupal 7 fully supports entities. 

Sub Users are entities as well, with the Relation module connecting the users.

The relationships that exist are deep and abstract to Views and, while it may be possible to do this with SQL, the resulting view would be very complex and inelegant.

So your question is more about how to construct a view with the right series of relationships so that you are given a complete list of orders out of a set of users. The most straightforward way to do this is to create a view of orders (so the base table is the order table) and then to use the contextual filters to provide a list of owner user_id's based on the currently logged in user using PHP.



Hint: You could use the following (UNTESTED) php snippet that makes use of a subuser function to provide an array of user ids:

// Updated with a tested bit of code:

&lt;?php
// never use the global $user variable directly. 
$account = $GLOBALS['user'];
// the subusers module abstracts this relationship easily for us
$subusers_children = subuser_load_all($account);
// views requires a return statement.
// the subuser_load_all() returns full objects, we just need the uids
$allusers = array_keys($subusers_children);
$allusers[] = $account-&gt;uid;
return implode(',',$allusers);


Here's an export of a view that works (sort of). The user permissions for this would have to be reworked to make it secure. Works with user 1 if user 1 has subusers and those subusers have orders.



// Edit 2: Did some more testing, found the view wasn't working and after some debugging, it started working correctly, limiting the users to our sub users group. Sorry about the weird problems initially introduced.

&lt;?php
$view = new view();
$view-&gt;name = 'user_orders_with_subusers';
$view-&gt;description = 'Display a list of completed orders for a user.';
$view-&gt;tag = 'commerce';
$view-&gt;base_table = 'commerce_order';
$view-&gt;human_name = 'User orders with subusers';
$view-&gt;core = 7;
$view-&gt;api_version = '3.0';
$view-&gt;disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Defaults */
$handler = $view-&gt;new_display('default', 'Defaults', 'default');
$handler-&gt;display-&gt;display_options['title'] = 'All orders';
$handler-&gt;display-&gt;display_options['use_more_always'] = FALSE;
$handler-&gt;display-&gt;display_options['access']['type'] = 'none';
$handler-&gt;display-&gt;display_options['cache']['type'] = 'none';
$handler-&gt;display-&gt;display_options['query']['type'] = 'views_query';
$handler-&gt;display-&gt;display_options['query']['options']['query_comment'] = FALSE;
$handler-&gt;display-&gt;display_options['exposed_form']['type'] = 'basic';
$handler-&gt;display-&gt;display_options['pager']['type'] = 'full';
$handler-&gt;display-&gt;display_options['pager']['options']['items_per_page'] = 25;
$handler-&gt;display-&gt;display_options['style_plugin'] = 'table';
$handler-&gt;display-&gt;display_options['style_options']['columns'] = array(
'order_number' =&gt; 'order_number',
'created' =&gt; 'created',
'changed' =&gt; 'changed',
'commerce_order_total' =&gt; 'commerce_order_total',
'status' =&gt; 'status',
);
$handler-&gt;display-&gt;display_options['style_options']['default'] = 'order_number';
$handler-&gt;display-&gt;display_options['style_options']['info'] = array(
'order_number' =&gt; array(
'sortable' =&gt; 1,
'default_sort_order' =&gt; 'desc',
'align' =&gt; '',
'separator' =&gt; '',
'empty_column' =&gt; 0,
),
'created' =&gt; array(
'sortable' =&gt; 1,
'default_sort_order' =&gt; 'desc',
'align' =&gt; '',
'separator' =&gt; '',
'empty_column' =&gt; 0,
),
'changed' =&gt; array(
'sortable' =&gt; 1,
'default_sort_order' =&gt; 'desc',
'align' =&gt; '',
'separator' =&gt; '',
'empty_column' =&gt; 0,
),
'commerce_order_total' =&gt; array(
'sortable' =&gt; 0,
'default_sort_order' =&gt; 'asc',
'align' =&gt; '',
'separator' =&gt; '',
'empty_column' =&gt; 0,
),
'status' =&gt; array(
'sortable' =&gt; 1,
'default_sort_order' =&gt; 'asc',
'align' =&gt; '',
'separator' =&gt; '',
'empty_column' =&gt; 0,
),
);
/* No results behavior: Global: Text area */
$handler-&gt;display-&gt;display_options['empty']['text']['id'] = 'text';
$handler-&gt;display-&gt;display_options['empty']['text']['table'] = 'views';
$handler-&gt;display-&gt;display_options['empty']['text']['field'] = 'area';
$handler-&gt;display-&gt;display_options['empty']['text']['content'] = 'You have not placed any orders with us yet.';
$handler-&gt;display-&gt;display_options['empty']['text']['format'] = 'plain_text';
/* Field: Commerce Order: Uid */
$handler-&gt;display-&gt;display_options['fields']['uid']['id'] = 'uid';
$handler-&gt;display-&gt;display_options['fields']['uid']['table'] = 'commerce_order';
$handler-&gt;display-&gt;display_options['fields']['uid']['field'] = 'uid';
/* Field: Commerce Order: Order number */
$handler-&gt;display-&gt;display_options['fields']['order_number']['id'] = 'order_number';
$handler-&gt;display-&gt;display_options['fields']['order_number']['table'] = 'commerce_order';
$handler-&gt;display-&gt;display_options['fields']['order_number']['field'] = 'order_number';
$handler-&gt;display-&gt;display_options['fields']['order_number']['link_to_order'] = 'customer';
/* Field: Commerce Order: Line items */
$handler-&gt;display-&gt;display_options['fields']['commerce_line_items']['id'] = 'commerce_line_items';
$handler-&gt;display-&gt;display_options['fields']['commerce_line_items']['table'] = 'field_data_commerce_line_items';
$handler-&gt;display-&gt;display_options['fields']['commerce_line_items']['field'] = 'commerce_line_items';
$handler-&gt;display-&gt;display_options['fields']['commerce_line_items']['settings'] = array(
'view' =&gt; 'commerce_line_item_table|default',
);
$handler-&gt;display-&gt;display_options['fields']['commerce_line_items']['delta_offset'] = '0';
/* Field: Commerce Order: Created date */
$handler-&gt;display-&gt;display_options['fields']['created']['id'] = 'created';
$handler-&gt;display-&gt;display_options['fields']['created']['table'] = 'commerce_order';
$handler-&gt;display-&gt;display_options['fields']['created']['field'] = 'created';
$handler-&gt;display-&gt;display_options['fields']['created']['label'] = 'Created';
/* Field: Commerce Order: Updated date */
$handler-&gt;display-&gt;display_options['fields']['changed']['id'] = 'changed';
$handler-&gt;display-&gt;display_options['fields']['changed']['table'] = 'commerce_order';
$handler-&gt;display-&gt;display_options['fields']['changed']['field'] = 'changed';
/* Field: Commerce Order: Order total */
$handler-&gt;display-&gt;display_options['fields']['commerce_order_total']['id'] = 'commerce_order_total';
$handler-&gt;display-&gt;display_options['fields']['commerce_order_total']['table'] = 'field_data_commerce_order_total';
$handler-&gt;display-&gt;display_options['fields']['commerce_order_total']['field'] = 'commerce_order_total';
$handler-&gt;display-&gt;display_options['fields']['commerce_order_total']['label'] = 'Total';
$handler-&gt;display-&gt;display_options['fields']['commerce_order_total']['click_sort_column'] = 'amount';
$handler-&gt;display-&gt;display_options['fields']['commerce_order_total']['settings'] = array(
'calculation' =&gt; FALSE,
);
/* Field: Commerce Order: Order status */
$handler-&gt;display-&gt;display_options['fields']['status']['id'] = 'status';
$handler-&gt;display-&gt;display_options['fields']['status']['table'] = 'commerce_order';
$handler-&gt;display-&gt;display_options['fields']['status']['field'] = 'status';
/* Contextual filter: Commerce Order: Uid */
$handler-&gt;display-&gt;display_options['arguments']['uid']['id'] = 'uid';
$handler-&gt;display-&gt;display_options['arguments']['uid']['table'] = 'commerce_order';
$handler-&gt;display-&gt;display_options['arguments']['uid']['field'] = 'uid';
$handler-&gt;display-&gt;display_options['arguments']['uid']['default_action'] = 'default';
$handler-&gt;display-&gt;display_options['arguments']['uid']['default_argument_type'] = 'php';
$handler-&gt;display-&gt;display_options['arguments']['uid']['default_argument_options']['code'] = '$account = $GLOBALS[\'user\'];
$subusers_children = subuser_load_all($account);
$allusers = array_keys($subusers_children);
$allusers[] = $account-&gt;uid;
return implode(\',\',$allusers);';
$handler-&gt;display-&gt;display_options['arguments']['uid']['summary']['number_of_records'] = '0';
$handler-&gt;display-&gt;display_options['arguments']['uid']['summary']['format'] = 'default_summary';
$handler-&gt;display-&gt;display_options['arguments']['uid']['summary_options']['items_per_page'] = '25';
$handler-&gt;display-&gt;display_options['arguments']['uid']['break_phrase'] = TRUE;
/* Filter criterion: Commerce Order: Order state */
$handler-&gt;display-&gt;display_options['filters']['state']['id'] = 'state';
$handler-&gt;display-&gt;display_options['filters']['state']['table'] = 'commerce_order';
$handler-&gt;display-&gt;display_options['filters']['state']['field'] = 'state';
$handler-&gt;display-&gt;display_options['filters']['state']['operator'] = 'not in';
$handler-&gt;display-&gt;display_options['filters']['state']['value'] = array(
'cart' =&gt; 'cart',
'checkout' =&gt; 'checkout',
);

/* Display: User Orders */
$handler = $view-&gt;new_display('page', 'User Orders', 'order_page');
$handler-&gt;display-&gt;display_options['path'] = 'orders-subusers';
$handler-&gt;display-&gt;display_options['menu']['title'] = 'All Orders';
$handler-&gt;display-&gt;display_options['menu']['weight'] = '15';
$handler-&gt;display-&gt;display_options['menu']['context'] = 0;
$handler-&gt;display-&gt;display_options['menu']['context_only_inline'] = 0;
$handler-&gt;display-&gt;display_options['tab_options']['type'] = 'normal';
$handler-&gt;display-&gt;display_options['tab_options']['title'] = 'Orders';
$handler-&gt;display-&gt;display_options['tab_options']['description'] = 'User orders in the store.';
$handler-&gt;display-&gt;display_options['tab_options']['weight'] = '';
$handler-&gt;display-&gt;display_options['tab_options']['name'] = 'user-menu';