Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Display Products by Lowest Price as default


Guest

Recommended Posts

You can change the sort order of the product columns asc or desc by product name, price, etc. The default seems to be by product name.

 

What do I have to change to Display Products by Lowest Price as default (on index.php)??

I want my customers to always see the lowest price items first.

 

This seems like it should be easy but I haven't found a solution myself or in the forums.

 

Thanks!

Link to comment
Share on other sites

You can change the sort order of the product columns asc or desc by product name, price, etc. The default seems to be by product name.

 

What do I have to change to Display Products by Lowest Price as default (on index.php)??

I want my customers to always see the lowest price items first.

 

This seems like it should be easy but I haven't found a solution myself or in the forums.

 

Thanks!

You can change the sort order by amending the SQL SELECT query. If you e.g. want to change the sort order on index.php you'd need to amend /includes/modules/new_products.php.

 

However - the new products module basically selects the latest products based on their date added ... now if you change to price you would not get the latest products, just the cheapest from the database, regardless of when they were added, meaning you could end up with very old, but very cheap products on your homepage.

 

To make this work, you'd therefore have to change the code to first select the latest products and then sort it by price. Looks like a rewrite of the whole code, might be worth checking in the contribution section first to see if e.g. "featured products" would be suitable for your shop.

 

all the best, Terra

 

PS: the "sort by price" works fine on the category pages as the products there are limited by the category - so only products from that category need to be sorted. For new_products it takes the products from the entire database, so if you sort by price without doing any other kind of script, you'd always have the cheapest products on the homepage which might not be a desired effect.

My code for combining PayPal IPN with ** QTPro 4.25 ** osC Affiliate ** CCGV(trad)

and how to solve the invoice already paid error

General info: Allow customer to delete order comment ** FTP Programs & Text Editors ** Amending order email **

Link to comment
Share on other sites

Maybe I missed something but if you're just talking about making price the default sort on the pages that already have a "sortable" display, i.e. the "category" index.php pages, then...

 

I tried this and it seems to work

 

lines 191-197 of index.php are

if ( (!isset($HTTP_GET_VARS['sort'])) || (!ereg('[1-8][ad]', $HTTP_GET_VARS['sort'])) || (substr($HTTP_GET_VARS['sort'], 0, 1) > sizeof($column_list)) ) {
  for ($i=0, $n=sizeof($column_list); $i<$n; $i++) {
	if ($column_list[$i] == 'PRODUCT_LIST_NAME') {
	  $HTTP_GET_VARS['sort'] = $i+1 . 'a';
	  $listing_sql .= " order by pd.products_name";
	  break;
	}

 

I just changed line 195 from

$listing_sql .= " order by pd.products_name";

to

$listing_sql .= " order by p.products_price";

 

The thing I noticed is it worked, but it did leave the "+" sorting "suffix" in front of "product name" in the table heading...so that would be something else to look at to finish off the switch.

 

I tried re-sorting it with the heading names after I changed it and it all still seemed to work. :blush:

Link to comment
Share on other sites

Excellent, thanks for the feedback. I tried variations of that but no that exact change. I'll give it a go.

Link to comment
Share on other sites

Excellent, thanks for the feedback. I tried variations of that but no that exact change. I'll give it a go.

 

 

Worked perfectly. Thanks again.

Link to comment
Share on other sites

Mark - you're a star! of course, so easy, if you just do it that way. There I is was considering writing a whole now script and all it takes is one change .... thanx for correcting my wrong advice and posting the right solution.

Terra

My code for combining PayPal IPN with ** QTPro 4.25 ** osC Affiliate ** CCGV(trad)

and how to solve the invoice already paid error

General info: Allow customer to delete order comment ** FTP Programs & Text Editors ** Amending order email **

Link to comment
Share on other sites

I just changed line 195 from

$listing_sql .= " order by pd.products_name";

to

$listing_sql .= " order by p.products_price";

 

I got the products to sort by price with the cheapest ones first - but I want the most expensive ones first. How can I do that? I tried changing

 

$listing_sql .= " order by p.products_price";

 

to

 

$listing_sql .= " order by p.products_price DESC";

 

but that only worked on the first page of the product listing. when I click to the next page goes back to the cheapest first.

 

any help? thanks!

Link to comment
Share on other sites

Hmm...I'm still very much a newbie at this, even though Terra made me feel like a hero :blush:

 

So there might be something wrong with my form here, but I did some experimenting.

 

For your price, highest first try this (lines 191-226 of index.php)...

 

if ( (!isset($HTTP_GET_VARS['sort'])) || (!ereg('[1-8][ad]', $HTTP_GET_VARS['sort'])) || (substr($HTTP_GET_VARS['sort'], 0, 1) > sizeof($column_list)) ) {
  for ($i=0, $n=sizeof($column_list); $i<$n; $i++) {
	if ($column_list[$i] == 'PRODUCT_LIST_PRICE') {
	  $HTTP_GET_VARS['sort'] = $i+1 . 'a';
	  $listing_sql .= " order by p. products_price DESC";
	  break;
	}
  }
} else {
  $sort_col = substr($HTTP_GET_VARS['sort'], 0 , 1);
  $sort_order = substr($HTTP_GET_VARS['sort'], 1);
  $listing_sql .= ' order by ';
  switch ($column_list[$sort_col-1]) {
	case 'PRODUCT_LIST_MODEL':
	  $listing_sql .= "p.products_model " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
	  break;
	case 'PRODUCT_LIST_NAME':
	  $listing_sql .= "pd.products_name " . ($sort_order == 'd' ? 'desc' : '');
	  break;
	case 'PRODUCT_LIST_MANUFACTURER':
	  $listing_sql .= "m.manufacturers_name " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
	  break;
	case 'PRODUCT_LIST_QUANTITY':
	  $listing_sql .= "p.products_quantity " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
	  break;
	case 'PRODUCT_LIST_IMAGE':
	  $listing_sql .= "pd.products_name";
	  break;
	case 'PRODUCT_LIST_WEIGHT':
	  $listing_sql .= "p.products_weight " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
	  break;
	case 'PRODUCT_LIST_PRICE':
	  $listing_sql .= "final_price " . ($sort_order == 'a' ? 'desc' : '') . ", pd.products_name";
	  break;
  }
}

 

To explain,

 

I changed

 

if ($column_list[$i] == 'PRODUCT_LIST_NAME')

 

to

 

if ($column_list[$i] == 'PRODUCT_LIST_PRICE')

 

That got the plus/minus sign in the correct column, to match up to our new sort order

 

AND I changed

 

case 'PRODUCT_LIST_PRICE':

$listing_sql .= "final_price " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";

break;

 

to

 

case 'PRODUCT_LIST_PRICE':

$listing_sql .= "final_price " . ($sort_order == 'a' ? 'desc' : '') . ", pd.products_name";

break;

 

And changing that 'd' to an 'a' seemed to solve the strange behavior for results past the first page.

 

 

I got the products to sort by price with the cheapest ones first - but I want the most expensive ones first. How can I do that? I tried changing

 

$listing_sql .= " order by p.products_price";

 

to

 

$listing_sql .= " order by p.products_price DESC";

 

but that only worked on the first page of the product listing. when I click to the next page goes back to the cheapest first.

 

any help? thanks!

Link to comment
Share on other sites

  • 4 weeks later...

Hi,

 

You might want to try replacing the entire block of code:

 

if ( (!isset($HTTP_GET_VARS['sort'])) || (!ereg('[1-8][ad]', $HTTP_GET_VARS['sort'])) || (substr($HTTP_GET_VARS['sort'], 0, 1) > sizeof($column_list)) ) {
  for ($i=0, $n=sizeof($column_list); $i<$n; $i++) {
	if ($column_list[$i] == 'PRODUCT_LIST_PRICE') {
	  $HTTP_GET_VARS['sort'] = $i+1 . 'a';
	  $listing_sql .= " order by p. products_price DESC";
	  break;
	}
  }
} else {
  $sort_col = substr($HTTP_GET_VARS['sort'], 0 , 1);
  $sort_order = substr($HTTP_GET_VARS['sort'], 1);
  $listing_sql .= ' order by ';
  switch ($column_list[$sort_col-1]) {
	case 'PRODUCT_LIST_MODEL':
	  $listing_sql .= "p.products_model " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
	  break;
	case 'PRODUCT_LIST_NAME':
	  $listing_sql .= "pd.products_name " . ($sort_order == 'd' ? 'desc' : '');
	  break;
	case 'PRODUCT_LIST_MANUFACTURER':
	  $listing_sql .= "m.manufacturers_name " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
	  break;
	case 'PRODUCT_LIST_QUANTITY':
	  $listing_sql .= "p.products_quantity " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
	  break;
	case 'PRODUCT_LIST_IMAGE':
	  $listing_sql .= "pd.products_name";
	  break;
	case 'PRODUCT_LIST_WEIGHT':
	  $listing_sql .= "p.products_weight " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
	  break;
	case 'PRODUCT_LIST_PRICE':
	  $listing_sql .= "final_price " . ($sort_order == 'a' ? 'desc' : '') . ", pd.products_name";
	  break;
  }
}

 

with:

 

$listing_sql .= " order by final_price";

 

This resolved the problem for me and sorted by price regardless of page number or manufacturer / category.

 

Garry

Garry

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...