Author Topic: Module VirtueMart Products - how to sort products ? (via php override?) [solved]  (Read 2574 times)


  • Beginner
  • *
  • Posts: 14

i am using Module VirtueMart Products with category filter, but i also need to sort displayed product (by SKU or better by value of product custom filed...)

I need to change sorting only in this module, so i cant use settings for sorting in back end... so its possible with default.php override? 


  • Beginner
  • *
  • Posts: 14
Re: Module VirtueMart Products - how to sort products ? (via php override?)
« Reply #1 on: November 27, 2012, 22:15:04 pm »
So i answer to myself ;D
(hope that its helps to somebody....)

there are probably three ways how to sort $products

1. make modification to VM core and sort products directly in sql select (via orderby) - complicated, causes problems with VM updates....
2. get data for products by youself via sql commands / own php code for selections - still complicated for me :)
3. sort already created $products (easy way - let VM to get sql data for you, then sorted it in few lines  ;) but with limitations... )
4. maybe some easier way, which i miss  :)

at the first: i am newbie in php programing, so i cant guarantie that my code is best way how to do it and also i dont test in large product database (i am using it in my module where are displayed max 20 products toagether...)

limitation : its sorts only product in $products  - so its depend on which products are in $products (mean which of them were selected and added here by default VM functions)

example :
you wants to sort products by price.
in category are products 1,2,3,4,5,6 
by VM module (or other function) you select and show only first 4 (mean only first 4 are added to $products)
so this function sort only this products (1,2,3,4) even if product 5 has lower price wouldnt be displayed....     

the code:


//sort function declaration - $products_to_sort=array of product to sort :) in VM defaults.php its "$products" $dir = way how to sort SORT_ASC or SORT_DESC //$value = "what sort via" (product_name,virtuemart_product_id,product_sku,prices..ect...) 
function sort_products_by_value(&$products_to_sort, $dir, $value) {
    $sort_col = array();
    foreach ($products_to_sort as $key=> $row) {
        $sort_col[$key] = $row->$value;
    array_multisort($sort_col, $dir, $products_to_sort);
//end sort funct. declaration

// sort function call - in my example i am sorting $products generated by defalt.php of module virtuemart products - 'prices' ($value) is  name of colmun in sql //virtuemart_product table
sort_products_by_value($products, SORT_ASC , 'prices');


//  original code continues (from defalut.php of module virtuemart products)

<div class="vmgroup<?php echo $params->get ('moduleclass_sfx') ?>">

   <?php if ($headerText) { ?>
   <div class="vmheader"><?php echo $headerText ?></div>
   if ($display_style == "div") {
      <div class="vmproduct<?php echo $params->get ('moduleclass_sfx'); ?> productdetails">
         <?php foreach ($products as $product) { ?>
         <div class="<?php echo $pwidth ?> <?php echo $float ?> ">
            <div class="spacer">
            <div style="text-align:center; margin-bottom:5px;">

/// bla bla bla... :)