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.

  1. So let’s take a look at ps_product_attribute.php
  2. Somewhere in there is a bunch of functions that start with ‘list_attribute’. Let’s look at those.
  3. 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)
  4. 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.
  5. 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'” ;
  6. 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.
  7. 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.
Be Sociable, Share!

Published by

Chris Malpass

I'm a web developer, photographer, casual gamer, and technophile from Virginia, USA.