Extending Virtuemart 1.1.4
Written by Thomas Kahl   
Saturday, 17 October 2009 08:36

As you might know, we always want to avoid changes in the Virtuemart code when we write addons or add features our customers need. Sure, this is not always possible. But in most cases, we find a solution without hacking the core code. The themes (available since 1.1.x) were a big step forward but Virtuemart 1.1.4 goes one step further.

We have added a new directory in the themes: /components/com_virtuemart/themes/your_theme_name/user_class

This directory can hold a replacement or extension of all classes that exist in /administrator/components/com_virtuemart/classes. Technically, Virtuemart checks if there is a file in the user_class-directory and uses this class to extend the original Virtuemart class. If such a file is not found, the original class is used without extension. This works similar to the Template Overrides in Joomla 1.5 - this is not the “clean” OOP-way, but this way, we could make sure that existing addons still work with this structure.

To extend Virtuemart is now much easier! Maybe you want to be informed when a new product was added:

Just create this new file: /components/com_virtuemart/themes/your_theme_name/user_class/ps_checkout.php and put the following code into the file:

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

class ps_product extends vm_ps_product {
	function add( &$d ) {
		$retval=vm_ps_product::add(&$d);
		if ($d['product_id']) {
			$message='A new product has been added to your shop:

ID: '.$d['product_id'].'
SKU: '.$d['product_sku'].'
Name: '.$d['product_name']; vmMail( " This e-mail address is being protected from spambots. You need JavaScript enabled to view it ", "ShopAdmin", " This e-mail address is being protected from spambots. You need JavaScript enabled to view it ", "New Product", $message, false, true); } } }

What does this code do?

  • The first line should be in every skript that is called within Joomla – this makes sure, that the code cannot be called directly.
  • In the second line, we create the class ps_product, which is the “original” classname. We extend the core virtuemart class vm_ps_product. That means, that we use everything from the core-class except the functions we have written on our own.
  • The third line creates the function “add”. This function already exists in the core class. We could have copied the whole function here and change the things we want to add. But because we want to use as much core code as possible, we call the original core-function in the fourth line. This way, we make sure that virtuemart does everything that is needed in the process.
  • In the fifth line we check, if a new product was created by checking if a product_id was created. If this is true, we can send an email to somebody to inform him/her that a new product was created. Of course you can add some code here to retreive the emailaddress dynamically. This is just an example.

As you can see, with some lines of code, we have added a new feature to Virtuemart without hacking the core code.

Security!

Everybody who has FTP-access to your site can add files to this user_class directory. Please keep this in mind. You can enable or disable the execution of the user_classes by setting the flag in the virtuemart-administration on the “security” tab. Please keep in mind, that you change the core behavior of virtuemart by adding user_classes. Be sure that your code is correct and secure – otherwise you might produce errors in your onlineshop.

Why in the themes?

You might ask, why the user_classes are stored in the themes directory and not in the admin-directorytree. That was discussed during the development. But i found it important, that everything that is changed or added for a customers shop is in one place. This way, you have all the custom development for your Virtuemart shop within one directory tree. Easy to backup and transport to another installation.

I hope this will be usefull for most Virtuemart developers and may help reduce the number of core-hacks out there.

Trackback(0)
Comments (8)Add Comment

Write comment
smaller | bigger

security code
Write the displayed characters


busy
 

Your Cart

Your Cart is currently empty.

Newsletter

Enter your email address:

Delivered by FeedBurner

Open Support Tickets


Work For Us

We are constantly searching for experienced virtuemart developers. Join our team! We have lots of work to do - from small addons to large enterprise projects. Contact us! 

Our Network

VM-Expert.com is part of the B01 Consulting Network. Since 2003 we build shops and websites with Joomla and Virtuemart. We are specialised in developing custom Joomla and Virtuemart Extensions.