Recently I worked on a project for a client and they wanted to customise their “Best Sellers” or Top Ten Modules appearance. The design came back with a product image, product price and a link to the product details. To achieve this in Virtuemart is not overly hard however it does have its inherit draw backs. Some include the fact you cannot have multiple module positions on your site for the top ten module. Instead you have to do the design for the one width / position then work your to the final product from there. This of course is for Joomla 1.5.x and Virtuemart 1.1.x. If you need a similar solution for older versions I will not be able to help as you should be upgrading your virtuemart store to the latest version along with your Joomla Install.

The problem I faced was that not one person had the knowledge or could be bothered to give a complete solution. This issue took a couple of days to work out a solution but once I had it nutted out it put me on easy street for the rest of the development. Anyway attached below is the code you need to achieve the desired outcome of what my client needed for their site. If you need help with it let  me know by posting a comment.

<?php

if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );

/*

* Best selling Products module for VirtueMart

* @version $Id: mod_virtuemart_topten.php 1160 2008-01-14 20:35:19Z soeren_nb $

* @package VirtueMart

* @subpackage modules

*

* @copyright (C) John Syben (john@webme.co.nz)

* Conversion to Mambo and the rest:

* 	@copyright (C) 2004-2005 Soeren Eberhardt

*

* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL

* VirtueMart is Free Software.

* VirtueMart comes with absolute no warranty.

*

* www.virtuemart.net

*----------------------------------------------------------------------

* This code creates a list of the bestselling products

* and displays it wherever you want

*----------------------------------------------------------------------

*/

global $mosConfig_absolute_path, $sess;

// Load the virtuemart main parse code

if( file_exists(dirname(__FILE__).'/../../components/com_virtuemart/virtuemart_parser.php' )) {

	require_once( dirname(__FILE__).'/../../components/com_virtuemart/virtuemart_parser.php' );

} else {

	require_once( dirname(__FILE__).'/../components/com_virtuemart/virtuemart_parser.php' );

}

require_once(CLASSPATH.'ps_product.php');

$ps_product = new ps_product;

// change the number of items you wanna haved listed via module parameters

$num_topsellers = $params->get ('num_topsellers', 10);

$list  = "SELECT p.product_id, p.product_sales as sales, p.product_parent_id, d.product_price, p.product_thumb_image, p.product_name, c.category_id, c.category_flypage ";

$list .= "FROM #__{vm}_product p, #__{vm}_product_price d, #__{vm}_product_category_xref pc, #__{vm}_category c WHERE ";

$q = "p.product_publish='Y' AND ";

$q .= "pc.product_id = p.product_id AND ";

$q .= "p.product_id = d.product_id AND ";

$q .= "pc.category_id = c.category_id AND ";

$q .= "p.product_sales > 0 ";

$q .= "GROUP BY p.product_id ";

$q .= "ORDER BY sales DESC, p.product_name ";

$list .= $q . " LIMIT 0, $num_topsellers "; 

$db = new ps_DB;

$db->query($list);

$tt_item=0;

$i = 0;

?>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<trcolor: rgb(0, 0, 0);"><?php echo $sectioncolor ?>">

<?php

  while ($db->next_record()) {
      if ($i == 0) {

          $sectioncolor = "sectiontableentry2";

          $i += 1;

      }

      else {

          $sectioncolor = "sectiontableentry1";

          $i -= 1;

      } 

      if( !$db->f('category_flypage') ) {

      	$flypage = ps_product::get_flypage( $db->f('product_id'));

      }

      else {

      	$flypage = $db->f('category_flypage');

      }

      $tt_item++;

      $pid = $db->f("product_parent_id") ? $db->f("product_parent_id") : $db->f("product_id");

$pricedisplay = $ps_product->show_price($db->f("product_id"));

      ?>

      <td width="141">
	<a title="<?php echo $db->p("product_name"); ?>" href="<?php  $sess->purl(URL . "index.php?page=shop.product_details&flypage=$flypage&product_id=" . $pid . "&category_id=" . $db->f("category_id")) ?>"><img src="components/com_virtuemart/shop_image/product/<?php $db->p("product_thumb_image");?>" border="0" /></a><br />
        <strong><?php echo $db->p("product_name"); ?></strong>
		<em><?php echo $pricedisplay; ?></em><a title="<?php echo $db->p("product_name"); ?>" href="<?php  $sess->purl(URL . "index.php?page=shop.product_details&flypage=$flypage&product_id=" . $pid . "&category_id=" . $db->f("category_id")) ?>">+ Product Details</a>

      </td>

    <?php 

  } ?>
   </tr>
</table>

<!--Top 10 End-->

If you would like a copy of the code to try you can get it by clicking here.

1 Response » to “Customise Top Ten Module Virtuemart”

  1. Jonathan Hilton says:

    NULTZ YOU ROCK!!!! Thank you so much for this!!! I have been looking for weeks for a solution to a similar problem with my Virtue Mart store.I also posted a request on the Virtue Mart forums and got no response from anyone.