Discussions

Cross-sell, Up-sell in drupal 7

Hello everybody,

I'am new in drupal commerce world, I need your help about how to use Cross-sell and Up-sell in drupal commerce, I searched all over the web, I concluded that is possible, but no clear method.

thank you in advance.

Posted: Dec 15, 2011

Comments

nimblematt on December 19, 2011

A big +1 to this!

I've been trying to find a relatively straightforward way to list related products inside my product displays and haven't really found any clear cut way to do it. Being relatively new to drupal, I initially chalked this up to my shaky understanding of basic drupal concepts, but after pouring through many different message boards, it seems like this has been a rarely-answered question.

My first instinct was to add a node reference field inside of my product listing (not product display) to point to other related product nodes. My thought was I could use these references inside of a block view, which would then list those related products (and a selection of their associated fields) on my product display.

I had minor success here. The view did display related products, but it would display all nodes selected as "related" irrespective of the current product being viewed. For example, if product A had products S, R, and T referenced as related, and product B had products U and V selected, products S, R, T, U and V would all be displayed on both product A and B's pages. Not what I'm looking for, clearly. After more reading, I realized this is probably happening because I'm using a block view to display the list, and contextual filtering doesn't work the same way here.

I've been wringing my hands over this one for a while now, and could really use a point in the right direction. Can I get this approach to work, or am I going the wrong way entirely?

Thanks for any help you can give!

nerdyit on February 21, 2012

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. I found this thread while looking for a solution myself. I am happy to answer any questions or suggestions for improvement.

Install the necessary modules

  1. Download, Install, and Enable the "Entity Reference" module. http://drupal.org/project/entityreference

Creating the Reference Field

  1. Create your product types and product display content types. For this example, the product display content types is labeled "Product Display".
  2. Navigate to Structure -> Content Types -> Product Display -> Manage fields
  3. 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.
  4. Choose the number of values. For this example, this is set to "Unlimited".
  5. Select the "Target type" of "Node".
  6. Select the target bundles. For this example, there is only one product display. Therefore, we highlight "Product Display".
  7. 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 dispaly

  1. Navigate to Structure -> Content Types -> Product Display -> Manage display
  2. 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

  1. If it hasn't already been done, add a few sample products, and the accompanying product displays.
  2. 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

  1. Initial View Creation
    1. Navigate to Structure -> Add new view
    2. Choose a view name. In this example, the name is "Related Products".
    3. Choose Show: "Content", of type "Product Display", sorted by "Newest first".
    4. Uncheck the box reading "Create a page"
    5. Check the box reading "Create a block".
    6. Give the block a title, for this example the title is "Related Products".
    7. Select the desired number of items to display and pager settings.
    8. Click "Continue & edit".
  2. Configure Filter Criteria
    1. In the "Filter criteria" heading, add a new filter of type "Global: PHP"
    2. In the "Filter code" section, add the following code:
        if (arg(0) && arg(0) == 'node') {
          $related_ids = array();
          $node = node_load(arg(1));
          $related = field_get_items('node',$node,'field_pd_related_products');
          if ($related && is_array($related) && sizeof($related) > 0) {
            for ($i=0; $i<=count($related); $i++ ) {
              $related_ids[] = $related[$i]['target_id'];
            }
          }
        }
        return ($related_ids && in_array($row->nid, $related_ids) ? FALSE : TRUE );
             
    3. Click on "Apply (all displays)"
  3. Configure Relationship from the Product Display to its Products
    1. Expand the "Advanced" settings of the view configuration.
    2. In the "Relationships" heading, add a new relationship to "Content: Referenced product".
    3. Give this reference an identifier, for this example "Product Reference" was used.
    4. Apply this setting. Fields from the product entity will now be available as fields in the view.
  4. Configure the Format
    1. In the "Format" heading, choose Format: "Unformatted list", Show: "Fields".
  5. Configure the Fields
    1. In the "Fields" heading, add the following fields:
      • Content: Nid
        • Choose "Exclude from display" for this field. The Node ID is only needed for the filter settings above.
      • Content: Title
      • Commerce Product: Image
        • Choose Relationship: "Product".
        • Uncheck Create a label.
        • Formatter: "Image"
        • Image Style: [your desired image style]
        • Link image to: "Content"
      • Commerce Product: Price
        • Choose Relationship: "Product".
        • Formatter: Formatted amount
        • Choose "Display the calculated sell price for the current user.".
  6. Configure Aggregation Settings
    1. In the "Advanced" heading, click on the link next to "Use aggregation".
    2. Check the "Aggregate" box.
  7. Save the View

Configure the Block

  1. Navigate to Structure -> Blocks.
  2. Locate the "View: Related Products" block.
  3. Choose the desired display region for this block. In this example, the region selected is "Second Sidebar" provided by the Zen theme.

Navigate to the Product Display that has Related Products to confirm everything worked.

Important Notes:

In the PHP code of the filter criteria, it is important that the field name matches the name of the field used as the entity reference. Also, the aggregation setting is required if there is more than one product referenced per product display node to avoid duplicate rows.

scott859@gmail.com Scott Jenkins on July 9, 2015

I know this is an old post but wanted to say that this apparently does not work with Commerce Kickstart2 - I'm using 7.x-2.25.

The main issue appears to be when you create the entity reference field - "Node" does not appear in the "Target Type".

If anyone has a solution of how to create a related products view for Commerce Kickstart2 - I would appreciate any insight you can give to this.