Jump to content

Sorting products by price by default, not name


Guest PLMaster

Recommended Posts

Guest PLMaster

In our product list, we have the option to sort by name or price, which we like. It currently defaults to sorting by name - we want the default sort order to be by price with highest price at the top. Can someone tell me how to do that?

Link to comment
Share on other sites

Ok open

includes/content/viewCat.inc.php

FIND:

$orderSort = sprintf(' ORDER BY name ASC');
 



(Highest to lowest)

REPLACE IT WITH:


$orderSort = sprintf(' ORDER BY price DESC');




If you would like to go the other way lowest price to highest price do this:

REPLACE IT WITH:


$orderSort = sprintf(' ORDER BY price ASC');

Save and upload

thats all ya need to do

Hope this helps

Kinetic :wacko:

In our product list, we have the option to sort by name or price, which we like. It currently defaults to sorting by name - we want the default sort order to be by price with highest price at the top. Can someone tell me how to do that?
Link to comment
Share on other sites

No problem Alan

CCv4 code is soooo much nicer even with Martins typos and inconsistencies

more "Object oriented" You can make big changes with little tweaking

At first I was a little peeved with the new code but after getting used it it was just about me being so used to CCv3 ways of doing things

I can now see the value and elegance of the way CCv4 does things.

I simply cannot wait to see what CCv5 has in "store" lol for us

Kinetic :)

Beautiful! I was also wondering about this.

Thanks for the help!

Link to comment
Share on other sites

  • 2 weeks later...
Guest jamble

just a quick couple of issues with this that I've come across on my client's shop ....

1. When you set it in this way, I've found that because I've got my categories set to list 10 products per page and that they have an odd number of products in one category (engagement rings here) that it seems to list the first page as having the odd number (ie. 9 are displaying on this page) and on the last page of the category (here) has the even number of products. Can anyone suggest how this can be altered to show the first 4 pages of this category having 10 products per page and the last page having the odd number?

2. Giving a little back ...

I noticed that by default when I listed prices lowest to highest, that the client also wanted products of the same price to show in alphabetical order (A-Z as opposed to the Z-A that the code above does) and to do this, simply edit the code detailled in Kinetic's post above to include the following:

$orderSort = sprintf(' ORDER BY price ASC, name ASC');

It's not much but I figured I'd share it anyway but if anyone can help on point 1 above I'd be grateful :)

Link to comment
Share on other sites

  • 2 weeks later...
Guest PLMaster

We upgraded and our customers are used to things being sorted by price automatically from high to low. Has anyone figured out how to make this work in 4.2.2 yet?

Link to comment
Share on other sites

  • 2 years later...

Ive been looking on the forums and havent found an answer to this old topic..

I dont have the answer but I think the code is now in the viewcat.inc.php in the includes folder, heres what Im guessing must be the code, how do we change it to default to price as the sort field?

## New! Product sorting by field

$allowedSort	= array('price', 'description', 'name', 'productCode', 'date_added');

if (isset($_GET['sort_by']) && in_array($_GET['sort_by'], $allowedSort)) {

	switch ($_GET['sort_order']) {

		case 'high':

			$orderType = 'DESC';

			$orderText = '&uarr';

			$sortIcon = 'bullet_arrow_up.gif';

			break;

		case 'low':

			$orderType = 'ASC';

			$orderText = '&darr';

			$sortIcon = 'bullet_arrow_down.gif';

			break;

		default:

			$orderType = 'ASC';

			$sortIcon = 'bullet_arrow_down.gif';

	}

	$orderSort = sprintf(' ORDER BY %s %s', $_GET['sort_by'], $orderType);

} else {

	if ($config['cat_newest_first']) {

		$orderSort = sprintf(' ORDER BY date_added DESC, name ASC');

	} else {

		$orderSort = false;

	}

}

Link to comment
Share on other sites

Hi

This should work. Just copy the code and paste it over yours on includes/content/viewCat.inc.php.


## New! Product sorting by field

$allowedSort	= array('price', 'description', 'name', 'productCode', 'date_added');

if (isset($_GET['sort_by']) && in_array($_GET['sort_by'], $allowedSort)) {

	switch ($_GET['sort_order']) {

		case 'high':

			$orderType = 'DESC';

			$orderText = '&uarr';

			$sortIcon = 'bullet_arrow_up.gif';

			break;

		case 'low':

			$orderType = 'ASC';

			$orderText = '&darr';

			$sortIcon = 'bullet_arrow_down.gif';

			break;

		default:

			$orderType = 'ASC';

			$sortIcon = 'bullet_arrow_down.gif';

	}

	   $orderSort = sprintf(' ORDER BY %s %s', $_GET['sort_by'], $orderType);

} else {

        if ($config['cat_newest_first']) {

                $orderSort = sprintf(' ORDER BY date_added DESC, price ASC, name ASC');

}else {

        if (!$config['cat_newest_first'] ) {       

        	$orderSort = sprintf('ORDER BY price ASC, name ASC');

        } else {

                $orderSort = false;

        }

}

}

In General Settings, in Admin Panel, you will need to set

Display newest products first

If enabled, the default category view will be to show the most recently added products first. To NO

This will now sort your products regardless of whether you have just added them, with the Price sorted low>high, then in ascending alphabeltical order.

This will NOT show the newest products first, which would be then sorted by Price low>high, then Alphabetically a>z, UNLESS you want it that way, then set the above to YES.

Lee

Link to comment
Share on other sites

I have been following this thread. My problem is slightly different. I want my products, by default, to appear with the most recent one added to be at the top of the page, not alphabetically as it does now. Can anyone tell me how to do this please.

Thanks

This configuration will only put the newest product at the top and not sorted by Price or Name, only if you have Display newest products first set to YES. If set to NO it will order by Price low > high, then alphabetically A > Z

## New! Product sorting by field

$allowedSort    = array('price', 'description', 'name', 'productCode', 'date_added');

if (isset($_GET['sort_by']) && in_array($_GET['sort_by'], $allowedSort)) {

        switch ($_GET['sort_order']) {

                case 'high':

                        $orderType = 'DESC';

                        $orderText = '&uarr';

                        $sortIcon = 'bullet_arrow_up.gif';

                        break;

                case 'low':

                        $orderType = 'ASC';

                        $orderText = '&darr';

                        $sortIcon = 'bullet_arrow_down.gif';

                        break;

                default:

                        $orderType = 'ASC';

                        $sortIcon = 'bullet_arrow_down.gif';

        }

           $orderSort = sprintf(' ORDER BY %s %s', $_GET['sort_by'], $orderType);

} else {

        if ($config['cat_newest_first']) {

                $orderSort = sprintf(' ORDER BY date_added DESC');

}else {

        if (!$config['cat_newest_first'] ) {       

                $orderSort = sprintf('ORDER BY price ASC, name ASC');

        } else {

                $orderSort = false;

        }

}

}

Hope that helps

Lee

Link to comment
Share on other sites

Thanks - first I didn't realise there was a 'Display Newest First' option, but I have found that and ticked it and still the products appear in alphabetical order (which in itself is strange). so I changed the code as you suggested above and it has certainly taken the products out of alphabetical order, and they do start to appear in order of latest ID, but two problems:

First the latest ID is at the bottom of the page not the top and second, then if you follow the product IDs upwards when you get about half way up a page they appear random - not in product ID order.

Looking at your code, am I right in assuming (not being a coder myself!) that they should appear in date_added order? If so, that could be a problem because when I look at the database, since the upgrade all the dates are now zero.

So is there a way to make the products appear in latest ID order?

Many thanks

Link to comment
Share on other sites

Hi

To be honest, I'm not sure what has happened your end. I tried the code on a site I'm working on and it all worked fine. I then changed it back to the original, standard code, as that is how my client wants it. I have just reapplied the code once again and all is ok. I have even been into the database and all the dates are fine, and not all zero. The above code should just simply add the latest product to the top of the products list shown in any given category the customer clicks on, (With 'Display Newest First' option set to YES), regardless of price or name. Otherwise, if set to NO, the products will be listed with the lowest priced first, up the the highest. But if some products are the same price, they will then be sorted alphabetically, before going to the next higher price.

Further Note:

I think I have been mis-reading your post. Am I right in saying that you have previously done an upgrade, which has left the dates in the database, all zero? So, therefore you cannot sort them by date added, but want them sorting by Product ID (Which will auto increment)? Try this and let me know if it works.

## New! Product sorting by field

$allowedSort    = array('price', 'description', 'name', 'productCode', 'date_added');

if (isset($_GET['sort_by']) && in_array($_GET['sort_by'], $allowedSort)) {

        switch ($_GET['sort_order']) {

                case 'high':

                        $orderType = 'DESC';

                        $orderText = '&uarr';

                        $sortIcon = 'bullet_arrow_up.gif';

                        break;

                case 'low':

                        $orderType = 'ASC';

                        $orderText = '&darr';

                        $sortIcon = 'bullet_arrow_down.gif';

                        break;

                default:

                        $orderType = 'ASC';

                        $sortIcon = 'bullet_arrow_down.gif';

        }

           $orderSort = sprintf(' ORDER BY %s %s', $_GET['sort_by'], $orderType);

} else {

        if ($config['cat_newest_first']) {

                $orderSort = sprintf(' ORDER BY productId DESC');

}else {

        if (!$config['cat_newest_first'] ) {       

                $orderSort = sprintf('ORDER BY price ASC, name ASC');

        } else {

                $orderSort = false;

        }

}

}

Lee

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...