Categories

Ordering VirtueMart Child Items in Dropdowns

Ok, so a ton of people keep asking me about how to order child products in the dropdown list. I don’t develop with VirtueMart much anymore so the suggestions I give here are mere suggestions and have not been tested or put through any sort of trial. I’m just looking at the code and guessing at what to change.

This exploratory exercise uses VirtueMart 1.1.6 stable.

If it works for you let me know. If it kind of works for you but you fix something and it works better, let me know and I’ll update here.

So let’s take a look at ps_product_attribute.php

Somewhere in there is a bunch of functions that start with ‘list_attribute’. Let’s look at those.

function list_attribute seems to be the main one that calls the other variations. It looks like by default it calls up the drop down menu for attributes. (line 221)

Since that one is the default, let’s see if making modifications to any of its queries will give us the results we want. The function starts at line 241.

At line 273ish you should see a SQL query: $q = “SELECT product_id,product_name FROM #__{vm}_product WHERE product_parent_id=$product_id AND product_parent_id<>0 AND product_publish=’Y'” ;

This is the first query you’re going to play with. You can tack on any sort of ORDER BY directives that you wish:ex $q = “SELECT product_id,product_name FROM #__{vm}_product WHERE product_parent_id=$product_id AND product_parent_id<>0 AND product_publish=’Y’ ORDER BY product_name” ;
I just ordered the SQL results by product name. You can put in whichever column or set of columns you wish to order by. It’s up to you.

Now take this same principle and try it throughout the various ‘list_attribute’ functions and see if you can achieve the sorting results that you desire.

After many hours of frustration I found this to be the solution… go to the file you’ve indicated (administrator/components/com_virtuemart/classes/ps_product_attribute.php) and around line 263 where the select query starts add the order by to the end of that line.

To clarify, I am talking about ordering the values in the ‘advanced’ attribute drop-down list on the front-end product flypage:
I tried adding ORDER BY to the SELECT queries, which made no difference.
I then thought, ok let’s try ordering the resultant array of values just before the drop-down list html is constructed using a PHP asort() function. This seems to work on initial testing.
This is done in file /components/com_virtuemart/themes/default/template/product_details/includes/addtocart_advanced_attribute.tpl.php around line 4 and 26 (should do both cases)…
add asort($attribute[‘options_list’]);
immediately after foreach($attributes as $attribute) {