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.

Thanks to Du Vien Trang for creating updated files for VirtueMart 1.0.13a and 1.0.14!

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:

 

  1. global $_MAMBOTS;
  2. $_MAMBOTS->loadBotGroup( 'content' );
  3. $tmp_row = new stdClass();
  4. $tmp_params = new mosParameters('');
  5. $tmp_row->text = $text_to_process;
  6. $_MAMBOTS->trigger( 'onPrepareContent', array( &$tmp_row, &$tmp_params ), true );
  7. $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):

 

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

 

Right after that line, add the plugin calling code:


  1. global $_MAMBOTS;
  2. $_MAMBOTS->loadBotGroup( 'content' );
  3. $tmp_row = new stdClass();
  4. $tmp_params = new mosParameters('');
  5. $tmp_row->text = $template;
  6. $_MAMBOTS->trigger( 'onPrepareContent', array( &$tmp_row, &$tmp_params ), true );
  7. $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):


  1. mm_showMyFileName( __FILE__ );

 

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


  1. global $_MAMBOTS;
  2. $_MAMBOTS->loadBotGroup( 'content' );
  3. $plug_row = new stdClass();
  4. $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):


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

 

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


  1. $plug_row->text = $desc;
  2. $_MAMBOTS->trigger( 'onPrepareContent', array( &$plug_row, &$plug_params ), true );
  3. $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):
  1. echo $product_cell;

 

Right before it, add the following plugin code:


  1. $plug_row->text = $product_cell;
  2. $_MAMBOTS->trigger( 'onPrepareContent', array( &$plug_row, &$plug_params ), true );
  3. $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.