03 328 8646

Blog

Related items in Drupal Commerce (Commerce Kickstart)

There are two main ways to handle related items - without getting into complex matching - that apply as much in Drupal Commerce as any other system.

  1. Manually selecting items that relate to a given item
  2. Automatically selecting them by the grouping they belong to. In Drupal terms this is likely to be by their Taxonomy term(s) and this is the method we use below

Both are pretty simple to set up.

Manual related items

If you want maximum control over the items displayed then this is the method to go for - but it is time consuming and needs maintenance. If you require a set number of related items for each item then, apart from the initial set up time of entering them all, you will need to update each item as products are removed from the site.

How to set up manual related items in Commerce Kickstart:

  1. Add an Entity Reference field to your product type node - eg at admin/structure/types/manage/product-display/fields
  2. Select Autocomplete if you have a high number of products, and Check boxes/Radio buttons if you don't - you can easily change this later if you don't like your choice
  3. Save
  4. Set Target Type to Content
  5. Set Mode to Simple
  6. Set Target Bundles to Product Display
  7. Save Settings
  8. The only value you may want to change on the next page is Number of Values to change how many can be entered by users
  9. Save
  10. Select the Manage Display tab.
  11. Choose the display mode you want related items be shown on.
  12. Select Rendered entity
  13. Click the little cog on that line and change the View Mode to your preferred mode
  14. Save
  15. Go to a product and add some related items.
  16. View your site and add CSS as required.

If you need the content displayed as a block instead of a field then follow the instructions at https://www.drupal.org/node/1465920

Automated related items

This method selects items that share a taxonomy term. The advantage is that you have very low set up time and zero maintenance, the disadvantage is that you don't have complete control over the items that display on any given page so they may not be ideal for conversion.

How to set up automatic related items in Commerce Kickstart:

Paste the below into the 'Import' page on Views - admin/structure/views/import . This avoids a long click by click list.

$view = new view();
$view->name = 'related_items';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'Related items';
$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'] = 'You may also love';
$handler->display->display_options['css_class'] = 'view-display-products';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$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'] = 'some';
$handler->display->display_options['pager']['options']['items_per_page'] = '3';
$handler->display->display_options['style_plugin'] = 'list';
$handler->display->display_options['style_options']['row_class_special'] = FALSE;
$handler->display->display_options['style_options']['class'] = 'all-products';
$handler->display->display_options['row_plugin'] = 'fields';
/* Field: Content: Rendered Content */
$handler->display->display_options['fields']['rendered_entity']['id'] = 'rendered_entity';
$handler->display->display_options['fields']['rendered_entity']['table'] = 'views_entity_node';
$handler->display->display_options['fields']['rendered_entity']['field'] = 'rendered_entity';
$handler->display->display_options['fields']['rendered_entity']['label'] = '';
$handler->display->display_options['fields']['rendered_entity']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['rendered_entity']['element_default_classes'] = FALSE;
$handler->display->display_options['fields']['rendered_entity']['link_to_entity'] = 1;
$handler->display->display_options['fields']['rendered_entity']['display'] = 'view';
$handler->display->display_options['fields']['rendered_entity']['view_mode'] = 'product_list';
$handler->display->display_options['fields']['rendered_entity']['bypass_access'] = 0;
/* Contextual filter: Content: Nid */
$handler->display->display_options['arguments']['nid']['id'] = 'nid';
$handler->display->display_options['arguments']['nid']['table'] = 'node';
$handler->display->display_options['arguments']['nid']['field'] = 'nid';
$handler->display->display_options['arguments']['nid']['default_action'] = 'default';
$handler->display->display_options['arguments']['nid']['default_argument_type'] = 'node';
$handler->display->display_options['arguments']['nid']['summary']['number_of_records'] = '0';
$handler->display->display_options['arguments']['nid']['summary']['format'] = 'default_summary';
$handler->display->display_options['arguments']['nid']['summary_options']['items_per_page'] = '25';
$handler->display->display_options['arguments']['nid']['not'] = TRUE;
/* Contextual filter: Content: Has taxonomy term ID (with depth) */
$handler->display->display_options['arguments']['term_node_tid_depth']['id'] = 'term_node_tid_depth';
$handler->display->display_options['arguments']['term_node_tid_depth']['table'] = 'node';
$handler->display->display_options['arguments']['term_node_tid_depth']['field'] = 'term_node_tid_depth';
$handler->display->display_options['arguments']['term_node_tid_depth']['default_action'] = 'default';
$handler->display->display_options['arguments']['term_node_tid_depth']['default_argument_type'] = 'taxonomy_tid';
$handler->display->display_options['arguments']['term_node_tid_depth']['default_argument_options']['term_page'] = FALSE;
$handler->display->display_options['arguments']['term_node_tid_depth']['default_argument_options']['node'] = TRUE;
$handler->display->display_options['arguments']['term_node_tid_depth']['default_argument_options']['limit'] = TRUE;
$handler->display->display_options['arguments']['term_node_tid_depth']['default_argument_options']['vocabularies'] = array(
'collections' => 'collections',
);
$handler->display->display_options['arguments']['term_node_tid_depth']['summary']['number_of_records'] = '0';
$handler->display->display_options['arguments']['term_node_tid_depth']['summary']['format'] = 'default_summary';
$handler->display->display_options['arguments']['term_node_tid_depth']['summary_options']['items_per_page'] = '25';
$handler->display->display_options['arguments']['term_node_tid_depth']['specify_validation'] = TRUE;
$handler->display->display_options['arguments']['term_node_tid_depth']['depth'] = '0';
$handler->display->display_options['arguments']['term_node_tid_depth']['break_phrase'] = TRUE;
/* Filter criterion: Content: Published */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'node';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = 1;
$handler->display->display_options['filters']['status']['group'] = 1;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: Content: Type */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'node';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['value'] = array(
'product_display' => 'product_display',
);

/* Display: Block */
$handler = $view->new_display('block', 'Block', 'block');

 

Once that is created you will probably need to make a couple of changes before it will work:

  1. Under 'Fields' Change the View Mode of the Content: Rendered Content  field to show a view mode that exists on your site
  2. Under Contextual Filters change Content: Has taxonomy term ID (with depth) to use a Vocabulary/Vocabularies that exist on your site

Navigate to the Blocks - admin/structure/block - page and set the block to display in your preferred location, and restrict it to pages, content types, roles etc as required.

 

If you have a simpler, better or more powerful way of handling related items please comment below, we'd love to hear from you.