Sort a related products view based on product features Hi I followed the intructions here for createa a view of related products for my commerce kickstart website, those related products are selected based on taxonomy vocabularies like texture, color, size, etc...

Right now the products are sorted by the title, but I do not want to sort them like that, I want to sort them based on a feature like color, texture, etc..

Allow me give you an example:

if I go the the product display of the product X that is of gray color, in the related products block I want to display first those that match the same color, so other related products that also have gray color will be displayed first.

Thanks for your help
If you want to display those product first that matches the current product features, include all those fields in view accordingly and then apply views_query_alter function to override the order and sort according to current product.

For example, If you want to sort according to product color taxonomy term, Include the field (Products referenced) Commerce Product: color (desc) in sort criteria of view. Then write following code in module file

function MYMODULE_views_query_alter(&amp;$view, &amp;$query) {
  // condition for current view
  if($view-&gt;name == 'related_products') {
    //Get current node product color tid &amp; override order query to sort according to that id
    $node = node_load(arg(1));
    $product_id = $node-&gt;field_product_reference[LANGUAGE_NONE]['0']['product_id'];
    $product = commerce_product_load($product_id);
    $query-&gt;orderby[0]['field'] = "commerce_product_field_data_field_product_reference__field_data_field_product_color.field_product_color_tid = '".$product-&gt;field_product_color[LANGUAGE_NONE]['0']['tid']."'";
  }
}


Let me know if you face any query regarding this.

Thanks!