Although Virtuemart offers downloadable goods out-the-box this isn’t always ideal. Say for example you run a premium (paid) video or article service and you don’t just want your visitors to be able to download the content they have bought but want them to be able to view it directly on your site whilst displaying a teaser to those who haven’t paid yet.
This would normally be quite a task but using a library called JomGenius that comes with Metamod & Chameleon it’s actually pretty simple!
1. Create custom fields in Virtuemart
First of all we need to create custom fields in Virtuemart to hold the premium content. If you’re using phpMyAdmin, open your database with it and find the jos_vm_products table. Create 2 new fields, for this example I’m going to call them product_premium_content and product_premium_teaser.
Alternatively you can create them with the following SQL command:
2. Include custom fields in Virtuemart admin template
Now that we have our new custom fields in the Virtuemart database we need to be able to edit their contents from within Virtuemart. To do so open administrator/components/com_virtuemart/html/product.product_form.php and around line 233 find:
Insert the following straight below:
To include the new fields in Virtuemart’s SQL statements when creating or updating products open /administrator/components/com_virtuemart/classes/ps_product.php and find the following command (twice) around line 310 and line 513:
insert this code below it, making sure you do so for both the add() and update() function
Now it’s already possible to start populating Virtuemart with your products from the back-end, they just wont appear on the front-end of your website yet.
3. Modify Virtuemart product details template
Next insert the premium content where you want it displayed on your product details page, in my case /components/com_virtuemart/themes/default/templates/product_details/flypage.tpl.php
The following example displays how you could use this for a pay-on-demand video site:
As you can see in my example I’m embedding a video using if (defined(“ALREADY_PURCHASED”)) to determine the URL of the flash file I’m displaying, depending on if the current user has bought the item or not.
Feel free to adapt it to your needs as you could use the same method to display any other type of media such as a audio or video playlist or even an app (by including a php file). The possibilities are endless…
4. Use Chameleon to check if user has purchased
This is the magical part. We need to set the variable which decides if the current user is allowed to see our premium content or not. This part is actually suprisingly easy! Just download ChameleonLite and install it in your Joomla! backend.
Now navigate to Components -> ChameleonLite -> Rules and create a new Rule with the following content:
Job done. We have a catalog of premium content which only displays a teaser until the content has been unlocked by the customer. A great thanks to Stephen from metamodpro.com for integrating this feature in JomGenius!
A note to sh404sef users: make sure the Chameleon plugin executes AFTER sh404sef.
Posted in Tutorials