Joomla! Plugins in VirtueMart

Joomla! plugins (mambots) are great! Show a video, play some music, protect an email address, or format that nifty quote. Unfortunately, content plugins don’t work by default in VirtueMart 1.0.x. This hack lets you run Joomla! plugins in VirtueMart pages.

The files modified in this tutorial are available for download in the Projects section of this site for those of you who just want the results but are not interested in the details. The modifications are marked with the text, gregdev. All modifications are done with respect to VirtueMart 1.0.9.

Calling Joomla! Plugins

There are three main steps required to call a Joomla! plugin:

  1. Get access to the global $_MAMBOTS object.
  2. Load a plugin group (in our case, content plugins).
  3. Trigger an event.

Thus, the code for running some text, say $text_to_process, through the Joomla! content plugins would look like this:

global $_MAMBOTS;
$_MAMBOTS->loadBotGroup( 'content' );
$tmp_row = new stdClass();
$tmp_params = new mosParameters('');
$tmp_row->text = $text_to_process;
$_MAMBOTS->trigger( 'onPrepareContent', array( &$tmp_row, &$tmp_params ), true );
$text_to_process = $tmp_row->text;

So, to make plugins work in VirtueMart pages, we just need to add this code to the VirtueMart files where we’d like the plugins to be called.

Be sure to back up the files before making any changes. All the files that you’ll need to edit are located in the /administrator/components/com_virtuemart/html directory.

Product Details Pages

To show plugins on your product details pages, you’ll need to edit shop.product_details.php. Look for the following line, near the bottom of the file (line 394):

$template = preg_replace("/{vm_lang:([^}]*)}/ie", "\$VM_LANG->\\1", $template);

Right after that line, add the plugin calling code:

global $_MAMBOTS;
$_MAMBOTS->loadBotGroup( 'content' );
$tmp_row = new stdClass();
$tmp_params = new mosParameters('');
$tmp_row->text = $template;
$_MAMBOTS->trigger( 'onPrepareContent', array( &$tmp_row, &$tmp_params ), true );
$template = $tmp_row->text;

Notice that the text we are running the plugins on is contained in the $template variable.

Now just call the plugin from your product description (the long description) in VirtueMart.

Shop Browse Pages

You might also want to run a plugin on the shop browse pages – for example, on a category description or a product’s short description. To do this, you’ll need to modify shop.browse.php.

Near the top of the file, look for this line (19):

mm_showMyFileName( __FILE__ );

Right after this add just the code that loads the plugins:

global $_MAMBOTS;
$_MAMBOTS->loadBotGroup( 'content' );
$plug_row = new stdClass();
$plug_params = new mosParameters('');

Notice that we’re loading the plugin group at the beginning of the file, but not actually triggering the plugins yet. We’ll trigger the plugins later on the category descriptions and short product descriptions separately.

Now, to run plugins on category descriptions, look for these two lines (83-84):

echo '<div style="width:100%;float:left;">';
echo $desc;

Right between those two lines, add this code to trigger the plugins:

$plug_row->text = $desc;
$_MAMBOTS->trigger( ‘onPrepareContent’, array( &$plug_row, &$plug_params ), true );
$desc = $plug_row->text;

The variable $desc contains the category description.
Let’s also make plugins work for the short product descriptions that appear on the shop browse pages. In the same file, shop.browse.php, look for this line (476):

echo $product_cell;

Right before it, add the following plugin code:

$plug_row->text = $product_cell;
$_MAMBOTS->trigger( ‘onPrepareContent’, array( &$plug_row, &$plug_params ), true );
$product_cell = $plug_row->text;

The variable $product_cell contains the short product description.

Now just add the appropriate plugin call to a category description or short product description in VirtueMart, and you’ll have Joomla! plugins displayed in VirtueMart’s shop browse pages.

Let me know which plugins you’ve been able to get working in VirtueMart! Just add it to the list of comments below. If you have problems getting a particular plugin to work with VirtueMart, or if you have problems with the code, let me know that too.