2

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 screenshot

The result should be the following: the one table like at the screenshot but with different (specified) users - the current user and his subusers...

  • by edit views you can add relationship there you find sibling relation need to apply. in fields section add field of sibling user. – vgoradiya Jan 5 '16 at 12:32
  • and so I will have two same fields for different users. But how can I have the one table with fields for many users... I tried to make an aggregation but it doesn't help – drru Jan 5 '16 at 12:43
  • Aggregation won't work because you have different fields ... Either you can create your own fields using Global: Text and multiple field tokens "[user_order][user_relation_order]" or you can use the template layer and override the row template to group the fields as you want in a table or otherwise. – joshmiller Jan 5 '16 at 14:50
  • may I use views_fieldset? – drru Jan 5 '16 at 15:32
  • I tried with tokens but the view doesn't show the orders of subusers. It show only the current user's orders.... How can I add subusers and their order in one table? The result should be the one table like at the screenshot but with different (specified) users - the current user and his subusers... – drru Jan 5 '16 at 15:38
2

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.

example screenshot of view with contextual filter

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:

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

screenshot of working subusers view

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

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

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

/* Display: User Orders */
$handler = $view->new_display('page', 'User Orders', 'order_page');
$handler->display->display_options['path'] = 'orders-subusers';
$handler->display->display_options['menu']['title'] = 'All Orders';
$handler->display->display_options['menu']['weight'] = '15';
$handler->display->display_options['menu']['context'] = 0;
$handler->display->display_options['menu']['context_only_inline'] = 0;
$handler->display->display_options['tab_options']['type'] = 'normal';
$handler->display->display_options['tab_options']['title'] = 'Orders';
$handler->display->display_options['tab_options']['description'] = 'User orders in the store.';
$handler->display->display_options['tab_options']['weight'] = '';
$handler->display->display_options['tab_options']['name'] = 'user-menu';
| improve this answer | |
  • thank you very much for your answer! unfortunately, this code doesn't work for me neither with inclusion nor wtihout it... there are some errors which drupal shows me not. Could you, please, test it or edit? I don't know, how can I improve it. I'm not as strong and experienced in PHP as you, so, I hope, you'll be kind to help me... – drru Jan 6 '16 at 9:45
  • it gives the following error after this code executed: Warning: html_entity_decode() expects parameter 1 to be string, array given в decode_entities() – drru Jan 6 '16 at 11:42
  • it gives the following error after this code executed: Warning: html_entity_decode() expects parameter 1 to be string, array given в decode_entities(). I made vardump and it showed that the subusers are stored in the variable. – drru Jan 6 '16 at 11:51
  • views doesn't allow miltiple value despite the active checkbox in "more". It lets me add some one specified user's order to print but no multiple values. how can I solve it? – drru Jan 6 '16 at 14:02
  • updated with working example – joshmiller Jan 6 '16 at 16:56

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.