Give the gift of Drupal. All merchandise is 50% off through 2016.
How to Create a Product Display Entity Reference and Block View of Related Products
This will guide you through creating a Related Products block for Drupal Commerce.
Install the necessary modules
- Download, Install, and Enable the "Entity Reference" module. http://drupal.org/project/entityreference
Creating the Reference Field
- Create your product types and product display content types. For this example, the product display content types is labeled "Product Display".
- Navigate to Structure -> Content Types -> Product Display -> Manage fields
- Add a new Entity Reference field. For this example, the field is labeled "Related Products", and named "pd_related_products". The widget type is Autocomplete.
- Choose the number of values. For this example, this is set to "Unlimited".
- Select the "Target type" of "Node" or "Content".
- Select the target bundles. For this example, there is only one product display. Therefore, we highlight "Product Display".
- Optionally, set the sort options. For this example, the field is sorted by the Title property in ascending order.
Configure the "Related Products" field in the display
- Navigate to Structure -> Content Types -> Product Display -> Manage display
- For this example, the Related Products are shown in a block. Therefore, we hide this field from the display.
Add one or more Related Products to a Product Display
- If it hasn't already been done, add a few sample products, and the accompanying product displays.
- On at least one product display edit screen, choose the references to the product displays that will be listed as Related Products.
Create the View
- Initial View Creation
- Navigate to Structure -> Add new view
- Choose a view name. In this example, the name is "Related Products".
- Choose Show: "Content", of type "Product Display", sorted by "Newest first".
- Uncheck the box reading "Create a page"
- Check the box reading "Create a block".
- Give the block a title, for this example the title is "Related Products".
- Select the desired number of items to display and pager settings.
- Click "Continue & edit".
- Configure Contextual Filters
- Add a new filter of "Content: Nid". Select "Provide default value", Change Type to "Content ID from URL"
- Configure Display
- Under "Format" set "Show" to be Fields
- Under "Fields" add "Content: Related Products". Set "Formatter" to be "Rendered Entity" and the view mode to your preferred mode. "Teaser" may be a good choice.
- Still under "Fields" remove "Content: title"
- Save the View
- Configure Filter Criteria - Only do this if you DON'T want the block to display when there are no results ie if no related products have been entered for this product
- Under "Filter Criteria" add "Content: Related Products (field_pd_related_products)" and set "Operator" to "Is not empty (NOT NULL)
- Under "Advanced" -> "Other" -> "Query settings" select "Distinct"
- Save the View
Configure the Block
- Navigate to Structure -> Blocks.
- Locate the "View: Related Products" block.
- Choose the desired display region for this block. In this example, the region selected is "Second Sidebar" provided by the Zen theme.