How can I sort the price divisions in the facet block numerically instead of by number of products in the division?

Right now it looks like this:
from $800.00 to $899.99 (6)
from $900.00 to $999.99 (6)
from $300.00 to $399.99 (5)
from $200.00 to $299.99 (5)
from $0.00 to $99.99 (4)
from $100.00 to $199.99 (1)

I would like this:
from $900.00 to $999.99 (6)
from $800.00 to $899.99 (6)
from $300.00 to $399.99 (5)
from $200.00 to $299.99 (5)
from $100.00 to $199.99 (1)
from $0.00 to $99.99 (4)

CommentFileSizeAuthor
#5 apachesolr_ubercart.module.patch1.13 KBwilldashwood

Comments

open social’s picture

Did you find any solution? Also interested in this.

smd_ksu’s picture

Nope, still holding out hope someone will post on how to do this.

open social’s picture

We ended up doing it in the theme override theme_apachesolr_facet_list

It is probably not the right way to go, but it will work for now.. good luck :-)

function mytemplate_apachesolr_facet_list($items, $display_limit = 0) {

  // change order uc price

  $keys = array_keys($items);
  $pos = strpos($keys[0], '€');

  // if there is an euro sign, assume we are editing the ubercart price filter
  if ($pos !== false) {
	foreach ($keys as $key => $item) {
		$buffer = explode('€', $item);
		$buf2 = explode(' ',$buffer[1]);
		$return_keys[str_replace('.', '', $buf2[0])] = $item;
	}
	ksort($return_keys);
	
	foreach($return_keys as $key => $item) {
		$return[$item] = $items[$item];	
	}
	$items = $return;
  } 
 
  $items = array_values($items);
  
  // If there is a limit and the facet count is over the limit, hide the rest.
  if (($display_limit > 0) && (count($items) > $display_limit)) {
    // Show/hide extra facets.
    drupal_add_js(drupal_get_path('module', 'apachesolr') . '/apachesolr.js');
    // Split items array into displayed and hidden.
    $hidden_items = array_splice($items, $display_limit);
    foreach ($hidden_items as $hidden_item) {
      if (!is_array($hidden_item)) {
        $hidden_item = array('data' => $hidden_item);
      }
      $hidden_item['class'] = isset($hidden_item['class']) ? $hidden_item['class'] . ' apachesolr-hidden-facet' : 'apachesolr-hidden-facet';
      $items[] = $hidden_item;
    }
  }
  $admin_link = '';
  if (user_access('administer search')) {
    $admin_link = l(t('Configure enabled filters'), 'admin/settings/apachesolr/enabled-filters');
  }
  return theme('item_list', $items) . $admin_link;
}

Put this code in template.php and replace mytemplate with your template machine name.

nick_vh’s picture

Sorry guys for the low support on this one! Im mostly focussed on the drupal 7 version of Apachesolr right now

willdashwood’s picture

StatusFileSize
new1.13 KB

Sorry to dig up this old thread. I was after the same thing but I decided to just do a quick and dirty hack of apachesolr_ubercart_price_facet_block. I wonder if it's worth considering making this an option in the module so people can choose between ordering by hit count or by cost.

I'm not very clued up on git or diff but I think I've made a patch against apachesolr_ubercart 6.x-1.x-dev if anyone is interested. I've just added the line:

$sortpre = $from_price;

and changed

ksort($items, SORT_STRING);

to:

ksort($items, SORT_NUMERIC);