Page 1 of 1

Change default products sort order in SPECIFIC category page

Posted: Tue Apr 22, 2014 6:33 pm
by guldan
Hello friends,

I'm using Opencart 1.5.4, clean install.
I have a category that I named "New Arrivals". The products in this category should be automatically sorted so that the newest products will be shown first.
After searching the forum, basically what I've found is to modify 2 lines on the controller/category.php like this:

change $sort = 'p.sort_order'; to $sort = 'p.date_added';
and change $order = 'ASC'; to $order = 'DESC';

Yes, it works BUT it affects ALL category pages!
Now all products in each category are sorted by default newest first.
Whereas, what I want is to apply it in "New Arrivals" category ONLY.

What I have in my mind, applying this to specific category will have something to do with category id (path).
My "New Arrivals" category id is 102. So I must first detect the current path. If it is 102 then apply sort by newest. If it is not 102, then ignore.
Then I tried to modify controller/category.php like this

Part 1, find this:

Code: Select all

$sort = 'p.sort_order';
Change to:

Code: Select all

if ($this->request->get['path'] == '102')
{
$sort = 'p.date_added';
}
$sort = 'p.sort_order';
Part 2, find this:

Code: Select all

$order = 'ASC';
Change to:

Code: Select all

if ($this->request->get['path'] == '102')
{
$order = 'DESC';
}
$order = 'ASC';
But it doesn't work. I'm not sure if the controller/category.php is the right file to modify.
Or it might be wrong lines (and wrong syntax)?

Please help me :)
Thank you..

Re: Change default products sort order in SPECIFIC category

Posted: Wed Apr 23, 2014 2:08 am
by Axansh
Small error in writing a code.

You should write like this :

if(condition){
---code--
}else{
--code--
}

if you write without else,everytime it overwrite previous condition.

Cheer!

Re: Change default products sort order in SPECIFIC category

Posted: Wed Apr 23, 2014 12:46 pm
by guldan
Axansh, thank you!!!!!
After adding "else" and its brackets, the code works!
Now the products are sorted newest-first ONLY on the intended category. The others categories are not affected.

Wow, what a shame. It turns out that I haven't yet graduated from that "if-else" basic lesson :))))