Thursday, November 7, 2013

Drupal Views Related Products

This method requires Views PHP in order to select the appropriate products.

There are many articles on this, but none worked perfectly for me, so I'm writing the steps I took.




1 ) In your product display, add a product reference field. I called mine related products.
2 ) Create a new view. I called mine Related Products.
3 ) Add whatever fields you want in the view. You probably want at least Content Path, Image, Title and Price.
4 ) Expand the Advance section.
5 ) Add Content: product reference relationship (under Relationships).
6 ) Add the Commerce Product: Product Id contextual filter. In that filter, do the following:
  1. Check provide default value.
  2. Select PHP Code for the type and enter the following code:
    $node=menu_get_object();
    $query = "";
    $or = "";
    if (isset($node->field_related_products) && count($node->field_related_products) > 0) {
    foreach ($node->field_related_products['und'] as $prodArray) {
    $query .= $or.$prodArray['product_id'];
    $or = "+";
    }
    }
    return $query;
  3. Scroll down, expand more and check Allow Multiple Values.
  4. Hit apply.
To include this view, you can call views_embed_view("related_products") anywhere in the product display template. If the product display you're viewing contains products in the related products field, they will show up.


2 comments:

  1. This technical post helps me to improve my skills set, thanks for this wonder article I expect your upcoming blog, so keep sharing..
    Regards,
    php training institutes

    ReplyDelete
  2. This technical post helps me to improve my skills set, thanks for this wonder article I expect your upcoming blog, so keep sharing..
    Regards,
    Web designing course in chennai|Web design training in chennai

    ReplyDelete