|
Anyone who's setup an online store with Virtuemart will know about the "Ask a question about this product" page being spammed. Opinion is divided between introducing a CAPTCHA system (requires a lot of core hacks) to removing the link all together. This is my solution which is somewhere between the two.
If the product "Ask a question" page is visited, this code alteration will ask the user to login, or register before asking a question.
This alteration only requires changes to shop.ask.tpl. Therefore you can safely upgrade VirtueMart without fear of loosing the changes.
Backup your files before you do this, or indeed any code change of any kind. You've been warned
Note: Ive attached to the bottom of this article a pre-modified version of shop.ask.tpl.php (zipped) for Virtuemart 1.1.4. Ive not tested it on other versions.
Load up the file /components/com_virtuemart/themes/<your theme>/templates/pages/shop.ask.tpl.php in your editor, you should see something like this
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
mm_showMyFileName( __FILE__ ); if ( $set == 0 ) { // This is the enquiry form! $validate = vmIsJoomla( '1.5' ) ? JUtility::getHash( $mainframe->getCfg( 'db' ) ) : mosHash( $mainframe->getCfg( 'db' ) ); ?> <br /> <a class="button" href="/<?php echo $product_link ?>"><?php echo $VM_LANG->_('VM_RETURN_TO_PRODUCT') ?></a> <br /><br /> <form action="<?php echo $mm_action_url ?>index.php" method="post" name="emailForm" id="emailForm"> <label for="contact_name"><?php echo $VM_LANG->_('NAME_PROMPT') ?></label> <br /><input type="text" name="name" id="contact_name" size="80" class="inputbox" value="<?php echo $name ?>"><br /><br /> <label for="contact_mail"><?php echo $VM_LANG->_('EMAIL_PROMPT') ?></label> <br /><input type="text" id="contact_mail" name="email" size="80" label="Your email" class="inputbox" value="<?php echo $email ?>"><br /><br /> <label for="contact_text"><?php echo $VM_LANG->_('MESSAGE_PROMPT') ?></label><br /> . . .
|
Continued on the next page.....
So we can see at line 20 there is a test to see if the form data has already been submitted. We're going to add a secondary condition, that is if the user is logged in, then display the form, otherwise invite the user to either login, or register.
Add the following lines to this file (at line 27)
Â
<?php $userinfo=& JFactory::getUser(); if(!$userinfo->guest) { ?>
Â
Your code should now look like this
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
mm_showMyFileName( __FILE__ ); ?> <div class="ask-question"> <div class="ask-question-inner"> <?php if ( $set == 0 ) { // This is the enquiry form! $validate = vmIsJoomla( '1.5' ) ? JUtility::getHash( $mainframe->getCfg( 'db' ) ) : mosHash( $mainframe->getCfg( 'db' ) ); ?> <a class="return-to-product" href="/<?php echo $product_link ?>"><?php echo $VM_LANG->_('VM_RETURN_TO_PRODUCT') ?></a> <?php $userinfo=& JFactory::getUser(); if(!$userinfo->guest) { ?> <form action="<?php echo $mm_action_url ?>index.php" method="post" name="emailForm" id="emailForm"> <label for="contact_name"><?php echo $VM_LANG->_('NAME_PROMPT') ?></label> <br /><input type="text" name="name" id="contact_name" size="80" class="inputbox" value="<?php echo $name ?>"><br /><br /> <label for="contact_mail"><?php echo $VM_LANG->_('EMAIL_PROMPT') ?></label> <br /><input type="text" id="contact_mail" name="email" size="80" label="Your email" class="inputbox" value="<?php echo $email ?>"><br /><br /> <label for="contact_text"><?php echo $VM_LANG->_('MESSAGE_PROMPT') ?></label><br /> <textarea rows="10" cols="60" name="text" id="contact_text" class="inputbox"><?php echo $subject ?></textarea><br />
|
Now move down to line 50 (ish), you should see this code:
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
<script type="text/javascript"><!-- function validateEnquiryForm() { if ( ( document.emailForm.text.value == "" ) || ( document.emailForm.email.value.search("@") == -1 ) || ( document.emailForm.email.value.search("[.*]" ) == -1 ) ) { alert( "<?php echo $VM_LANG->_('CONTACT_FORM_NC',false); ?>" ); } else if ( ( document.emailForm.email.value.search(";") != -1 ) || ( document.emailForm.email.value.search(",") != -1 ) || ( document.emailForm.email.value.search(" ") != -1 ) ) { alert( "You cannot enter more than one email address" ); } else { document.emailForm.action = "<?php echo sefRelToAbs("index.php"); ?>" document.emailForm.submit(); } } --></script> <?php } else { // if set==1 then we .... ..
|
Add the following lines, straight after line 61 in the above snippett. You'll need to get rid of the existing opening <?php tag.
Code to insert:
66 67 68 69 70 71 72
|
<?php } else { ?> Please <A href="/user/login.html">Login to ask a question</A>. If you do not have an account, <a href="/index.php?option=com_virtuemart&page=shop.registration&Itemid=54"> you can register here</a>. <?php }
|
So it should now look like this
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
<script type="text/javascript"><!-- function validateEnquiryForm() { if ( ( document.emailForm.text.value == "" ) || ( document.emailForm.email.value.search("@") == -1 ) || ( document.emailForm.email.value.search("[.*]" ) == -1 ) ) { alert( "<?php echo $VM_LANG->_('CONTACT_FORM_NC',false); ?>" ); } else if ( ( document.emailForm.email.value.search(";") != -1 ) || ( document.emailForm.email.value.search(",") != -1 ) || ( document.emailForm.email.value.search(" ") != -1 ) ) { alert( "You cannot enter more than one email address" ); } else { document.emailForm.action = "<?php echo sefRelToAbs("index.php"); ?>" document.emailForm.submit(); } } --></script> <?php } else { ?> Please <A href="/user/login.html">Login to ask a question</A>. If you do not have an account, <a href="/index.php?option=com_virtuemart&page=shop.registration&Itemid=54">you can register here</a>. <?php } } else { // if set==1 then we have sent the email to the vendor and say thank you here. ?> <img src="/<?php echo VM_THEMEURL ?>images/button_ok.png" height="48" width="48" align="center" alt="Success" border="0" /> <?php echo $VM_LANG->_('THANK_MESSAGE') ?> . . .
|
Â
Save your file, upload it if you have to. Browse to one of your products and click on "Ask a question about this product" If you're logged in, it will present the form as normal, if you're not, we'll then it will ask you to login or register. There is an argument to say the user may decide they cant be bothered to login, or register, and a Captcha is far more suitable. I agree, and I've no doubt that the rather spiffing chaps at Virtuemart will put this feature in to the next release. Until then, Im opting for this method, and making registering / logging in easier for my customers by using our EmailAsUsername Plugin
If you're using the default Virtuemart template, Ive attached a pre-modified version. Just to stress, this is for Virtuemart 1.1.4. I havent tested it with any other versions.
 |