2

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

2
  • doesn't it work in sort criteria?
    – GwenM
    Commented May 21, 2015 at 15:51
  • how how can I add a sort criteria that sort the related products that match the color first?
    – svelandiag
    Commented May 22, 2015 at 16:36

3 Answers 3

1

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

Let me know if you face any query regarding this.

Thanks!

1
  • Finally a decent answer, I will try it soon and let you know. Thanks!
    – svelandiag
    Commented May 30, 2015 at 21:55
0

I think contextual filters will help you. You can filter a view based on some arguments coming from the node on which the view is included. I don't have any experience with commerce kickstart, but this is a pure views functionality. See here: http://dannyenglander.com/blog/using-views-contextual-filters-create-related-content-block-drupal-7

1
  • Hi maybe I was not clear enough, I already created a view of related products, now I want to sort the related products that match the color first. Please read my question...
    – svelandiag
    Commented May 22, 2015 at 16:37
0

I think this is not sort issue. It's not a normal sort. Also I can't imagine how to do that in views UI. You may need to write your own code by using entityfieldquery.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.