I'm using Drupal Commerce, so there are Product Displays, and product variations. I created index for Product Displays, and indexed also some fields of my variations, but I cannot see them in sorts tab. :( They are decimal numeric fields so I thought it should be possible to sort by them, am I doing something wrong? Is it not possible to sort by fields of referenced entities? If so - how it is possible to sort by price?

Comments

luke_nuke’s picture

The fact that one sort for price have "(Max)" and second "(Min)" at the end, has something to do with min and max values of price for multi value entity reference that is product? If so how can I create that kind of sorts for fields in objects under multi value entity references? (programatically at least)

Anonymous’s picture

Are these multi-value fields? You can't sort multi-value fields. So you would need to add a field with a single value that you wish to sort by.

luke_nuke’s picture

Situation is like this:
Product Display has multi-value Product reference field, price is a field of Product, not Product Display, Product Display is indexed by Search API, and there is possibility of indexing Min and Max price from all possible products in Product reference field of Product Display. So the fields alone are not multi-value, but there is "multi-valuity" on the way. This has something to do with indexing and I'm wondering how is it possible to index other fields just the way price is, to be able to sort by them. I would gladly imitate the way the price was enabled in sorts, but I don't know where is a code for it. As I think about it now, it is more question about how to index fields of entities in multi-value field of reference, so If you think it would be better, I can post in in "search api" issues, but it is somehow related to sorts, and is important mainly for using sorts so my first thought was to ask here.

stijn.blomme’s picture

I had the exact same problem and fixed it by adding a 'sort price' property to my node entity.
This field contains the lowest price of the referenced products.
The sort price property can be indexed by search api and sorted by search api sorts.



/*
 * Implements hook_entity_property_info_alter
 * 
 */

function example_entity_property_info_alter(&$info){
  $info['node']['properties']['sort_price'] = array(
     'type' => 'decimal',
     'label' => 'Sort price',
     'description' => 'The price to sort this product by.',
     'getter callback' => '_example_get_sort_price', 
  ); 
 
}

/*
 * getter callback for sort price property
 * 
 * return the lowest product price associated with this display
 * 
 */
function _example_get_sort_price($data, array $options, $name, $type, $info){
  if($name == 'sort_price'){
    $wrapper = entity_metadata_wrapper('node', $data);
    //check if this is a product display
    if(array_key_exists('field_product', $wrapper->getPropertyInfo())){
      $prices = array();
      //loop trough referenced products
      foreach ($wrapper->field_product->getIterator() as $delta => $term_wrapper) {
        //get value of commerce_price
        $price_field = $term_wrapper->commerce_price->value();
        //transform commerce price to decimal       
        $prices[] = commerce_currency_amount_to_decimal($price_field['amount']);
      }
      //return lowest price for this display
      return min($prices);
    }
  }
  return NULL;
}


Anonymous’s picture

Status: Active » Fixed

#4 fixes the issue hopefully?

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

borisson_’s picture

Issue summary: View changes

This can be fixed by adding the search_api_ranges module and enabling the filter 'Search API ranges' on the field you want to sort on.

pslcbs’s picture

#4 works for me as @stijn.blomme said

Thanks a lot!!

interdruper’s picture

If you build an aggregated field on the index using the UI, there is no need to add a custom property (#4) or install additional modules. Just go to 'Filters' tab, enable aggregated fields and add a new one of type 'Minimum' over the Price field ('amount').