Discussions

List Purchased Products for Current User

Hello,

I've been trying to get this to work for about a week and can't crack it. Using Drupal 7, with Commerce 1.8.

The website has digital products and videos that people can purchase. Each user gets access only to what they've purchased.

The setup:

1. Content Type "Purchased Video" created for each purchasable digital product. Actual product data is contained in this content.

2. Commerce Product Type "Video" created, with an entity reference field to the related "Purchased Video" page.

When a user completes a purchase, they are granted "View" permission to each Purchased Video in their order.

Now, what I'm having trouble with is displaying the "library" of already purchased videos for the currently logged in user. It should just be a list of links to the users available Purchased Videos.

Thus, I create a View page. Base table is line items. Then add relationship line item:product, line item:order id, and Order: Owner. Add contextual filter on Commerce Order: Uid, default value is currently logged in user. Then the field used in the view is simply the Purchased Video page title (linked).

Full view export is below.

I followed this post that was very helpful: http://www.drupalcommerce.org/discussions/8648/list-products-bought-user

Unfortunately, it's not really working. Instead, I get a blank result set when I test this with an actual customer account. It does, however, work properly with the admin account AND the view preview works like a charm too. Even when I input my test users ID as the contextual filter.

Seems sort of like a permissions issue to me because admin doesn't have a problem. But I have no clue which permission needs attention. Then again, maybe I haven't configured the view correctly.

Sort of new to Drupal too - which doesn't help. Don't be shy with technical stuff though.

$view = new view();
$view->name = 'another_my_video_page';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'commerce_line_item';
$view->human_name = 'Another My Video Page';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Another My Videos';
$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['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '10';
$handler->display->display_options['style_plugin'] = 'table';
$handler->display->display_options['style_options']['columns'] = array(
  'line_item_id' => 'line_item_id',
  'field_clw_recipe_videoref' => 'field_clw_recipe_videoref',
);
$handler->display->display_options['style_options']['default'] = '-1';
$handler->display->display_options['style_options']['info'] = array(
  'line_item_id' => array(
    'sortable' => 0,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
  'field_clw_recipe_videoref' => array(
    'sortable' => 0,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
);
$handler->display->display_options['style_options']['empty_table'] = TRUE;
/* Header: Global: PHP */
$handler->display->display_options['header']['php']['id'] = 'php';
$handler->display->display_options['header']['php']['table'] = 'views';
$handler->display->display_options['header']['php']['field'] = 'php';
$handler->display->display_options['header']['php']['empty'] = TRUE;
$handler->display->display_options['header']['php']['php_output'] = '<p>Current User Id: <strong><?php global $user; echo $user->uid; ?></strong></p>
';
/* Relationship: Commerce Line item: Referenced products */
$handler->display->display_options['relationships']['commerce_product_product_id']['id'] = 'commerce_product_product_id';
$handler->display->display_options['relationships']['commerce_product_product_id']['table'] = 'field_data_commerce_product';
$handler->display->display_options['relationships']['commerce_product_product_id']['field'] = 'commerce_product_product_id';
$handler->display->display_options['relationships']['commerce_product_product_id']['label'] = 'Products';
$handler->display->display_options['relationships']['commerce_product_product_id']['required'] = TRUE;
/* Relationship: Commerce Line Item: Order ID */
$handler->display->display_options['relationships']['order_id']['id'] = 'order_id';
$handler->display->display_options['relationships']['order_id']['table'] = 'commerce_line_item';
$handler->display->display_options['relationships']['order_id']['field'] = 'order_id';
$handler->display->display_options['relationships']['order_id']['required'] = TRUE;
/* Relationship: Commerce Order: Owner */
$handler->display->display_options['relationships']['uid']['id'] = 'uid';
$handler->display->display_options['relationships']['uid']['table'] = 'commerce_order';
$handler->display->display_options['relationships']['uid']['field'] = 'uid';
$handler->display->display_options['relationships']['uid']['relationship'] = 'order_id';
$handler->display->display_options['relationships']['uid']['required'] = TRUE;
/* Field: Commerce Line Item: Line item ID */
$handler->display->display_options['fields']['line_item_id']['id'] = 'line_item_id';
$handler->display->display_options['fields']['line_item_id']['table'] = 'commerce_line_item';
$handler->display->display_options['fields']['line_item_id']['field'] = 'line_item_id';
/* Field: Commerce Product: Purchased Video Page */
$handler->display->display_options['fields']['field_clw_recipe_videoref']['id'] = 'field_clw_recipe_videoref';
$handler->display->display_options['fields']['field_clw_recipe_videoref']['table'] = 'field_data_field_clw_recipe_videoref';
$handler->display->display_options['fields']['field_clw_recipe_videoref']['field'] = 'field_clw_recipe_videoref';
$handler->display->display_options['fields']['field_clw_recipe_videoref']['relationship'] = 'commerce_product_product_id';
$handler->display->display_options['fields']['field_clw_recipe_videoref']['settings'] = array(
  'link' => 1,
);
/* 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']['relationship'] = 'order_id';
$handler->display->display_options['arguments']['uid']['default_action'] = 'default';
$handler->display->display_options['arguments']['uid']['default_argument_type'] = 'current_user';
$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';

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'another-my-video-page';
Posted: Apr 29, 2014

Comments

info on April 30, 2014

The solution dawned on me this morning... It's stupid simple too.

Instead of messing with Commerce at all, I just created a view on the Purchased Video content itself. Then link the title to the actual Purchased Video page. Drupal takes care of the rest. Including hiding Purchased Video content that the current user does not have view access for.