Virtuemart coupon system mod
Continuing on the virtuemart modifications series, this post is to help people who would like some basic features in the coupon code system, which allow for user specific, product specific and minimum value of order based discounts. This modification has been done for Joomla 1.5.6 + Virtuemart 1.1.2. This should be valid for the 1.5.x and 1.1.x series, but no guarantees!
Before I begin and as much as I’d like to say that this is a very safe modification to do, if you do not understand php / sql do not proceed and even if you do, it is always best to backup your installation and database.
To start things off, we need to first be able to add the above values to the coupon from the administration panel. For that, open the following file:
administrator/components/com_virtuemart/html/coupon.coupon_form.php
and before the end of the form, that is before the </table> and the <tr> group add the following code:
<!-- Modification to add support for additional coupon code options -->
<tr>
<td width="24%"><div align="right"><?php echo $VM_LANG->_('PHPSHOP_COUPON_PRODUCT_ID') ?>:</div></td>
<td width="76%"> <input type="text" class="inputbox" name="product_id" value="<?php $db->sp("product_id"); ?>" /> </td>
</tr>
<tr>
<td width="24%"><div align="right"><?php echo $VM_LANG->_('PHPSHOP_COUPON_USER_ID') ?>:</div></td>
<td width="76%"> <input type="text" class="inputbox" name="user_id" value="<?php $db->sp("user_id"); ?>" /> </td>
</tr>
<tr>
<td width="24%"><div align="right"><?php echo $VM_LANG->_('PHPSHOP_COUPON_MIN_VALUE') ?>:</div></td>
<td width="76%"> <input type="text" class="inputbox" name="min_value" value="<?php $db->sp("min_value"); ?>" /> </td>
</tr>
<tr>
<td width="24%"><div align="right"><?php echo $VM_LANG->_('PHPSHOP_COUPON_DISC_TYPE') ?>:</div></td>
<td width="76%"> <input type="radio" class="inputbox" name="discount_type" value="overall" <?php if($db->sf("discount_type")=='overall' || empty($coupon_id)) echo "checked=\"checked\""; ?> /> <?php echo $VM_LANG->_('PHPSHOP_COUPON_DISC_OVERALL_TYPE') ?> <?php echo mm_ToolTip( $VM_LANG->_('PHPSHOP_PRODUCT_DISCOUNT_TYPE_TIP') ); ?><br /> <input type="radio" class="inputbox" name="discount_type" value="specific" <?php if($db->sf("discount_type")=='specific') echo "checked=\"checked\""; ?> /> <?php echo $VM_LANG->_('PHPSHOP_COUPON_DISC_SPECIFIC_TYPE') ?> </td>
</tr>
<!-- End of modification-->
Now, if you see the coupons in the administration panel, you’ll see new fields but nothing will be there to show what those fields are for. That is okay because we haven’t added the language modifications to the language file.
For that, open the following file
administrator/components/com_virtuemart/languages/common/english.php
and add the following code anywhere. The options include the language sets for the modifications which we haven’t yet made, but since they’ll be needed we’ll just add them all together.
// custom messages for coupon code 'PHPSHOP_COUPON_PRODUCT_ID' => 'Product ID', 'PHPSHOP_COUPON_USER_ID' => 'User ID', 'PHPSHOP_COUPON_MIN_VALUE' => 'Minimum order value', 'PHPSHOP_COUPON_DISC_TYPE' => 'Discount type', 'PHPSHOP_PRODUCT_DISCOUNT_TYPE_TIP' => 'Overall discount gives discount to all products while specific gives discount only to that specific product. Makes sense only if you are selecting percentage discount type', 'PHPSHOP_COUPON_DISC_OVERALL_TYPE' => 'Overall', 'PHPSHOP_COUPON_DISC_SPECIFIC_TYPE' => 'Specific', 'PHPSHOP_PRODUCT_ID_NOT_NUMBER' => 'Product ID is not a number', 'PHPSHOP_USER_ID_NOT_NUMBER' => 'User ID is not a number', 'PHPSHOP_MIN_VALUE_NOT_NUMBER' => 'Minimum value is not a number', 'PHPSHOP_PRODUCT_ID_NEEDED' => 'Product ID is required when selecting specific discount type', 'PHPSHOP_REMOVE_COUPON' => 'Remove', // end of custom messages for coupon code
Now you should be seeing what the new fields are for in the coupons page in admin panel. However, if you save any changes, they’ll not be made because we haven’t added the code and haven’t modified the database table to hold those values. If you have phpmyadmin, then just add the following fields to ‘jos_vm_coupons’ table (jos_vm prefix might be different for you if you chose a different prefix while installing).
product_id, user_id, min_value, discount_type
All of the fields should be ‘text’ type. All of them should have default value as null and should be nullable. If you do not have phpmyadmin, search for sql syntax and add these tables as specified above.
Now, we need to add code to process these values from the coupons form. Go to
administrator/components/com_virtuemart/classes/ps_coupon.php
and then to the ‘add_coupon’ function. Add the following to the starting of the function:
$product_id = NULL;
$user_id = NULL;
$min_value = NULL;
if ((int)($d['product_id'])) {
$product_id = (int)$d['product_id'];
}
if ((int)($d['user_id'])) {
$user_id = (int)$d['user_id'];
}
if ((int)($d['min_value'])) {
$min_value = (float)$d['min_value'];
}
and add these lines to the ‘$fields => array(‘, at the ending part of it. Also, make sure you add a ‘,’ to the last value
'coupon_value' => (float)$d['coupon_value']
'product_id' => $product_id != 0 ? $product_id : NULL, 'user_id' => $user_id != 0 ? $user_id : NULL, 'min_value' => $min_value != 0 ? $min_value : NULL, 'discount_type' => strtolower($d['discount_type']) == 'overall' ? 'overall' : 'specific'
Make same modifications to the ‘update_coupon’ function. Now you should be able to make changes to your coupons in the admin panel and they should stay as per your modifications. As of now, the ‘product_id’ and ‘user_id’ have to be manually put in by looking at the database but later on, I plan to make things easier.
Now, we need to make sure that the coupon is processed as per our requirements. Go to the ‘process_coupon_code’ function in the same file. Modify the query variable ‘$q’ in this function to add the following variables
product_id, user_id, min_value, discount_type
and under
if ($coupon_db->num_rows() > 0)
{
add the following code
if ($coupon_db->f('product_id') != null) {
$cart = $_SESSION['cart'];
$return_status = 0;
for($i = 0; $i < $cart['idx']; $i++) {
if ($cart[$i]['product_id'] == $coupon_db->f('product_id')) {
$return_status = 1;
}
}
if ($return_status == 0) {
$GLOBALS['coupon_error'] = $VM_LANG->_('PHPSHOP_COUPON_CODE_INVALID');
return false;
}
}
$auth = $_SESSION['auth'];
if ($coupon_db->f('user_id') != null && $coupon_db->f('user_id') != $auth['user_id']) {
$GLOBALS['coupon_error'] = $VM_LANG->_('PHPSHOP_COUPON_CODE_INVALID');
return false;
}
if ($coupon_db->f('min_value') != null && round($d['total']) < $coupon_db->f('min_value')) {
$GLOBALS['coupon_error'] = $VM_LANG->_('PHPSHOP_COUPON_CODE_INVALID');
return false;
}
and below this, modify the existing
if ($coupon_db->f('percent_or_total') == 'percent')
{
/* percent */
//$subtotal = $checkout->calc_order_subtotal( $d );
/* take the subtotal for calculation of the discount */
$coupon_value = round( ($d['total'] * $coupon_db->f('coupon_value') / 100), 2);
if( $d['total'] < $coupon_value ) {
$coupon_value = (float)$d['total'];
$vmLogger->info( str_replace('{value}',$GLOBALS['CURRENCY_DISPLAY']->getFullValue( $coupon_value ),$VM_LANG->_('VM_COUPON_GREATER_TOTAL_SETTO')) );
}
$_SESSION['coupon_discount'] = $coupon_value;
}
to look like this
if ($coupon_db->f('percent_or_total') == 'percent')
{
/* percent */
//$subtotal = $checkout->calc_order_subtotal( $d );
/* take the subtotal for calculation of the discount */
if ($coupon_db->f('discount_type') == 'overall') {
$coupon_value = round( ($d['total'] * $coupon_db->f('coupon_value') / 100), 2);
if( $d['total'] < $coupon_value ) {
$coupon_value = (float)$d['total'];
$vmLogger->info( str_replace('{value}',$GLOBALS['CURRENCY_DISPLAY']->getFullValue( $coupon_value ),$VM_LANG->_('VM_COUPON_GREATER_TOTAL_SETTO')) );
}
$_SESSION['coupon_discount'] = $coupon_value;
} else {
$product_number = null;
$cart = $_SESSION['cart'];
for($i = 0; $i < $cart['idx']; $i++) {
if ($cart[$i]['product_id'] == $coupon_db->f('product_id')) {
$product_number = $i;
}
}
require_once(CLASSPATH.'ps_product.php');
$ps_product= new ps_product;
$price = $ps_product->get_adjusted_attribute_price($cart[$product_number]['product_id'], $cart[$product_number]['description']);
$product_price = $GLOBALS['CURRENCY']->convert( $price['product_price'], @$price['product_currency'] );
$coupon_calculation_value = $cart[$product_number]['quantity'] * $product_price;
$coupon_value = round( ($coupon_calculation_value * $coupon_db->f('coupon_value') / 100), 2);
if( $d['total'] < $coupon_value ) {
$coupon_value = (float)$d['total'];
$vmLogger->info( str_replace('{value}',$GLOBALS['CURRENCY_DISPLAY']->getFullValue( $coupon_value ),$VM_LANG->_('VM_COUPON_GREATER_TOTAL_SETTO')) );
}
$_SESSION['coupon_discount'] = $coupon_value;
}
}
else
{
$coupon_value = $coupon_db->f('coupon_value');
/* Total Amount */
if( $d['total'] < $coupon_value ) {
$coupon_value = (float)$d['total'];
$vmLogger->info( str_replace('{value}',$GLOBALS['CURRENCY_DISPLAY']->getFullValue( $coupon_value ),$VM_LANG->_('VM_COUPON_GREATER_TOTAL_SETTO')) );
}
$_SESSION['coupon_discount'] = $GLOBALS['CURRENCY']->convert( $coupon_value );
}
Now, for the last modification. This is actually something generic for virtuemart, wherein when a product is deleted, the coupon still remains. If a user adds a coupon and deletes the product and adds another product, the coupon should not remain. The following modification ensures that.
Open file
administrator/components/com_virtuemart/classes/ps_cart.php
and around line 442, where you should be seeing
$temp['idx'] = $j; $_SESSION['cart'] = $temp; ps_cart::saveCart();
add the following below the abovementioned code:
if ($_SESSION['coupon_id']) {
require_once(CLASSPATH.'ps_coupon.php');
$ps_coupon = new ps_coupon();
if (!$ps_coupon->process_coupon_code($d)) {
unset($_SESSION['coupon_discount']);
unset($_SESSION['coupon_id']);
unset($_SESSION['coupon_code']);
unset($_SESSION['coupon_type']);
unset($_SESSION['coupon_value']);
unset($_SESSION['coupon_redeemed']);
}
}
That’s it! You should now have the functionality of the coupons to be used with the features mentioned above! And that too with the niceness of native 1.5 code. Hope it helps a lot of people looking to add basic coupon functionality to their virtuemarts!
Update:- For those people having an issue with doubling coupon discounts (generic VM issue with 1.1.4), take a look at comment 118! Thanks for pointing it out George!
October 12th, 2008 18:43
Hi,
this is very promising. Have not yet tried it, but will do very soon. Would it be much work to add functionality to give discount per category?
Thom
October 12th, 2008 22:00
@Thom:
Do let me know your feedback if you give it a try. On your question, it’s not much work at all. You just need to proceed in the same fashion as product_id for category_id.
Let me know in case you need help!
October 13th, 2008 11:29
Hello,
This looks very good.
Could this though be modified to give customers a discount when the total amount of the order reaches a certain amount?
With kind regards,
Rutger
October 13th, 2008 12:17
@Rutger:
Yes, it can be done to do that automatically. But this system allows for coupon to be applicable only when order reaches a certain amount.
So I suppose if the customer has been notified of such a discount, it’s best to give him a coupon code.
Hope that helps!
October 13th, 2008 14:28
Hello,
My client want’s all customers to receive a discount when the total order amount reaches an certain price. Honestly, i have no idea how to achieve that. Client wants to be able to set this option from the background, to either percentage or fixed amount…..
Hope you can help me out!
October 13th, 2008 21:33
@Rutger:
I think I should be able to do that, but it’ll have to be a commercial job as it’s not a simple modification to the existing mod that I have provided above.
If you’ll be interested in that, let me know. You can use the contact form.
October 14th, 2008 02:06
Hi,
thanks for quick replies.
Looks like I’m a bit over my head, but got it partly to work.
There are some syntax errors in the code regarding &amp;&amp; types. – guess this should be && ?
Also a bit unclear on where to add the changes in ps_database.php as there was an if..else there.
I could use some explanation on what
‘user_id’ => $user_id != 0 ? $user_id : NULL,
actually means, as I never managed to get add and update on the coupon table to work properly for the fields allowing for NULL.
(set it to $user_id if not 0 and NULL if 0?)
Guess discount_type is an enum field in the database? That was not quite clear either.
I also has some problems with the “spesific” coupon code to be correct – seems like it gave 16% when coupon set to 20% and 20% when coupon set to 25%
Could this have to do with TAX/VAT calculations?
When it comes to my category version of this it is getting complicated. I guess i first need to (in my case) iterate the cart as you do in the product_id test then check for a match on product_id in the jos_vm_product_category_xref table, then check for a match for that category in the jos_vm_category_xref in case the product is in a child category of the coupon category_id.
And even if I got that to work I’m not sure if I understand the proper way to do it in the process_coupon code. Both where you check it to be valid and then again where you calculate the discount per product.
I think I understand much of the logic, but I’m unfortunately not versed enough in php or Joomla/Virtuemart to actually implement it.
If you have any pointers it will be much appreciated. And I really believe that this should have been part of the VM core functionality.
Keep up the good work.
October 14th, 2008 10:25
@Thom:
You’re right about the &s, I am not sure why they’re showing like that above, they’re supposed to be ‘&&’. I’ll try to get them corrected.
in the ps_database, it’s around line 346 and 365. The first change is in the ‘case REPLACE’ and the second one in the ‘case UPDATE’. If you need some help on this further, I could show it to you on Yugma.
The ‘user_id’ => $user_id != 0 ? $user_id : NULL, statement is to assign NULL value to $user_id if it is 0 and keep it to the non-zero value if it is not zero. One liner for an if statement. This is needed because the NULL value is not properly assigned sometimes and it turns out as 0 (due to the statements at the beginning of this function).
As for the SQL query, it goes something like this:
ALTER TABLE `jos_vm_coupons` ADD `test_field` TEXT NULL DEFAULT NULL ;
This creates a text type ‘test_field’ with null as default and nullable type. You should be able to modify this for other cases.
The discount_type is also a text field. I missed writing that in the above instructions
Again, you’re spot on with the discount calculations. The discount is made only on the actual price, not the taxed price. You can change this behavior by changing the price reading line for a product, if required.
As for the category_id, the method you’ve outlined should definitely work and it should not be much work.
I hope this helps you getting things up and running
October 21st, 2008 07:23
Thanks for your efforts on this. I’ve been trying to get the ps_database to work, but for some reason, this line seems to make it so you cannot enter a new product ‘elseif (!$value) {$q .= “NULL” . “\n”; }’ . If it’s commented out, it seems to work fine. If it’s there, it causes the error message ‘Something went wrong when trying to add the product!’ to pop up.
October 21st, 2008 09:05
@J Lee:
Thank you for your feedback! The problem was that without this modification, I was unable to set the field to null and there would be a ’0′ coming instead of blank, which is not how it should be.
It seems like setting it to null is not going to be the way to go. I’ll modify the code to work without NULL and write an update in the above post by the weekend. Although, that solution will have non-empty fields for the unset fields, but I guess it’s better than breaking virtuemart in general
Be sure to let me know if anything else is wrong!
October 28th, 2008 15:45
Hello,
Thank you very much for the fix and the instructions. I think I am nearly there but am having a problem with the ps_coupon.php file.
When I add or edit a coupon in the back end I get the following error message:
“Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ‘)’ in ../administrator/components/com_virtuemart/classes/ps_coupon.php on line 129″
Im not too good at PHP but I dont think Im missing a ‘)’
October 28th, 2008 16:10
@Colman:
You’re right about the solution, you need to add a ‘)’ at line 129, but I am not sure why you got this. From the looks of it, people were able to get this running (notwithstanding other issues…) from the above instructions.
In any case, do put in the missing ‘)’ and let me know if you’re able to get things running.
On a side note, I’ll be modifying the above code slightly (not in working, but implementation) soon. So be sure to update your install then!
October 28th, 2008 19:17
Hi,
I have tried adding that ‘)’ in but am still getting an error. Perhaps I have the chunk of code in the wrong place? Could you have a look please?
/* function to add a coupon coupon_code to the database */
function add_coupon_code( &$d ) {
/* CK edit */
$product_id = NULL;
$user_id = NULL;
$min_value = NULL;
if ((int)($d['product_id'])) {
$product_id = (int)$d['product_id'];
}
if ((int)($d['user_id'])) {
$user_id = (int)$d['user_id'];
}
if ((int)($d['min_value'])) {
$min_value = (float)$d['min_value'];
}
/* CK edit */
global $vmLogger, $VM_LANG;
$coupon_db =& new ps_DB;
if( !$this->validate_add( $d ) ) {
return false;
}
$fields = array(
‘coupon_code’ => vmGet($d,’coupon_code’),
‘percent_or_total’ => strtolower($d['percent_or_total']) == ‘percent’ ? ‘percent’ : ‘total’,
‘coupon_type’ => strtolower($d['coupon_type']) == ‘gift’ ? ‘gift’ : ‘permanent’,
‘coupon_value’ => (float)$d['coupon_value']
/* CK edit */
‘coupon_value’ => (float)$d['coupon_value']
‘product_id’ => $product_id != 0 ? $product_id : NULL,
‘user_id’ => $user_id != 0 ? $user_id : NULL,
‘min_value’ => $min_value != 0 ? $min_value : NULL,
‘discount_type’ => strtolower($d['discount_type']) == ‘overall’ ? ‘overall’ : ‘specific’
/* CK edit */
);
$coupon_db->buildQuery( ‘INSERT’, ‘#__{vm}_coupons’, $fields );
if( $coupon_db->query() ) {
$_REQUEST['coupon_id'] = $coupon_db->last_insert_id();
$vmLogger->info($VM_LANG->_(‘VM_COUPON_ADDED’));
return true;
}
return false;
}
October 29th, 2008 09:23
@Colman:
I don’t see any syntax errors in the above. If you’d like we can have a yugma session (netmeeting) and I can see what is wrong ?
October 30th, 2008 14:49
Hi,
My boss pointed out I was missing a ‘,’ and to try moving the comments to the top and that did the trick. It is working well but I will be testing it more in the coming weeks
Thanks for the offer anyway and thanks for the fix
November 16th, 2008 19:13
Hi,
first of thank you for this great hack/mod for VM.
I have a few questions though:
Modify the query variable ‘$q’ in this function to add the following variables
product_id, user_id, min_value, discount_type
how do you add these variables ?
I stated them as:
function process_coupon_code( $d ) {
global $VM_LANG, $vmLogger, $product_id, $user_id, $min_value, $discount_type;
is this correct ?
and the ;amp;amp;amp
How does this need to be changed correctly ?
Thankyou!
November 16th, 2008 19:19
And as an edit,
now im getting:
Error: The price could not be updated.
when trying to update my products.
Any idea how this happend ?
November 16th, 2008 21:46
@Jooshua:
This issue has been reported by Thom above too. Unfortunately I have not been able to get time to fix this. I’ll try to get to it on this weekend.
As for the ‘&s’, the sequence is for a ‘&&’. It got maligned when I copy pasted to the wordpress editor. (and I though I fixed them…).
In any case, let me get to the product update issue and in the meantime, you should use your original db file.
Hope that helps!
November 16th, 2008 23:12
@Admin,
The original DB file fixes the error indeed.
Though now the coupons don’t work. It won’t put a discount in. it stays at -0.00.
The coupon page does show; “0″ at the product, user id and minimum order value.
where could this be located?
and ill be looking forward to the fix for the update issue, because a coupon mod like this should be standard.
thanks in advance!
November 16th, 2008 23:21
again an edit:
Totals do work, percentages don’t.
Next thing is, that the coupon won’t “register” the minimum order value. it stays at 0.
thank you!
November 17th, 2008 09:16
@Jooshua:
The coupons will not update correctly because of the db class thing. It was a ‘fix’ that let things work out, but it turned out to be blowing the product updates.
I’ll try to get to the fix this weekend
November 18th, 2008 02:12
Hi,
I have been looking more into this. Maybe it´s what is commented on earlier by others, but I get a Critical: Adding the Order into the Database failed! User ID XX. With debug on it says that both `order_discount`and `coupon_discount`cannot be NULL. Guess that´s what we´re doing in the ps_database file for NULL values. I don´t dare to change the limitations in the table though.
Looking forward to your update
November 25th, 2008 14:32
@Admin
Dear Sir,
I am facing an error occuring at line 307 in my ps_coupon.
Please help me to fix the error.
I have attached my code
/* function to process a coupon_code entered by a user */
function process_coupon_code( $d ) {
global $VM_LANG, $vmLogger, $product_id, $user_id, $min_value, $discount_type;
/* init the database */
$coupon_db =& new ps_DB;
/* we need some functions from the checkout module */
require_once( CLASSPATH . “ps_checkout.php” );
$checkout =& new ps_checkout();
if( empty( $d['total'])) {
$totals = $checkout->calc_order_totals($d);
$d['total'] = $totals['order_subtotal']
+ $totals['order_tax']
+ $totals['order_shipping']
+ $totals['order_shipping_tax']
- $totals['payment_discount'];
}
$d['coupon_code'] = trim(vmGet( $_REQUEST, ‘coupon_code’ ));
$coupon_id = vmGet( $_SESSION, ‘coupon_id’, null );
$q = ‘SELECT coupon_id, coupon_code, percent_or_total, coupon_value, coupon_type FROM #__{vm}_coupons WHERE ‘;
if( $coupon_id ) {
/* the query to select the coupon coupon_code */
$q .= ‘coupon_id = ‘.intval($coupon_id);
}
else {
/* the query to select the coupon coupon_code */
$q .= ‘coupon_code = \”.$coupon_db->getEscaped( $d['coupon_code'] ).’\”;
}
/* make the query */
$coupon_db->query($q);
/* see if we have any fields returned */
if ($coupon_db->num_rows() > 0)
{
if ($coupon_db->f(‘product_id’) != null) {
$cart = $_SESSION['cart'];
$return_status = 0;
for($i = 0; $i f(‘product_id’)) {
$return_status = 1;
}
}
if ($return_status == 0) {
$GLOBALS['coupon_error'] = $VM_LANG->_(‘PHPSHOP_COUPON_CODE_INVALID’);
return false;
}
}
$auth = $_SESSION['auth'];
if ($coupon_db->f(‘user_id’) != null && $coupon_db->f(‘user_id’) != $auth['user_id']) {
$GLOBALS['coupon_error'] = $VM_LANG->_(‘PHPSHOP_COUPON_CODE_INVALID’);
return false;
}
if ($coupon_db->f(‘min_value’) != null && round($d['total']) f(‘min_value’)) {
$GLOBALS['coupon_error'] = $VM_LANG->_(‘PHPSHOP_COUPON_CODE_INVALID’);
return false;
}
/* we have a record */
/* see if we are calculating percent or dollar discount */
if ($coupon_db->f(‘percent_or_total’) == ‘percent’)
{
/* percent */
//$subtotal = $checkout->calc_order_subtotal( $d );
/* take the subtotal for calculation of the discount */
if ($coupon_db->f(‘discount_type’) == ‘overall’) {
$coupon_value = round( ($d['total'] * $coupon_db->f(‘coupon_value’) / 100), 2);
if( $d['total'] info( str_replace(‘{value}’,$GLOBALS['CURRENCY_DISPLAY']->getFullValue( $coupon_value ),$VM_LANG->_(‘VM_COUPON_GREATER_TOTAL_SETTO’)) );
}
$_SESSION['coupon_discount'] = $coupon_value;
} else {
$product_number = null;
$cart = $_SESSION['cart'];
for($i = 0; $i f(‘product_id’)) {
$product_number = $i;
}
}
require_once(CLASSPATH.’ps_product.php’);
$ps_product= new ps_product;
$price = $ps_product->get_adjusted_attribute_price($cart[$product_number]['product_id'], $cart[$product_number]['description']);
$product_price = $GLOBALS['CURRENCY']->convert( $price['product_price'], @$price['product_currency'] );
$coupon_calculation_value = $cart[$product_number]['quantity'] * $product_price;
$coupon_value = round( ($coupon_calculation_value * $coupon_db->f(‘coupon_value’) / 100), 2);
if( $d['total'] info( str_replace(‘{value}’,$GLOBALS['CURRENCY_DISPLAY']->getFullValue( $coupon_value ),$VM_LANG->_(‘VM_COUPON_GREATER_TOTAL_SETTO’)) );
}
$_SESSION['coupon_discount'] = $coupon_value;
}
}
else
{
$coupon_value = $coupon_db->f(‘coupon_value’);
/* Total Amount */
if( $d['total'] info( str_replace(‘{value}’,$GLOBALS['CURRENCY_DISPLAY']->getFullValue( $coupon_value ),$VM_LANG->_(‘VM_COUPON_GREATER_TOTAL_SETTO’)) );
}
$_SESSION['coupon_discount'] = $GLOBALS['CURRENCY']->convert( $coupon_value );
}
else
{
$coupon_value = $coupon_db->f(“coupon_value”);
/* Total Amount */
if( $d["total"] info( str_replace(‘{value}’,$GLOBALS['CURRENCY_DISPLAY']->getFullValue( $coupon_value ),$VM_LANG->_(‘VM_COUPON_GREATER_TOTAL_SETTO’)) );
}
$_SESSION['coupon_discount'] = $GLOBALS['CURRENCY']->convert( $coupon_value );
}
/* mark this order as having used a coupon so people cant go and use coupons over and over */
$_SESSION['coupon_redeemed'] = true;
$_SESSION['coupon_id'] = $coupon_db->f(“coupon_id”);
$_SESSION['coupon_code'] = $coupon_db->f(“coupon_code”);
$_SESSION['coupon_type'] = $coupon_db->f(“coupon_type”);
}
else
{
/* no record, so coupon_code entered was not valid */
$GLOBALS['coupon_error'] = $VM_LANG->_(‘PHPSHOP_COUPON_CODE_INVALID’);
return false;
}
}
}
?>
The line 307 should be the line which state,
# else
# {
#
# $coupon_value = $coupon_db->f(‘coupon_value’);
#
# /* Total Amount */
# if( $d['total'] info( str_replace(‘{value}’,$GLOBALS['CURRENCY_DISPLAY']->getFullValue( $coupon_value ),$VM_LANG->_(‘VM_COUPON_GREATER_TOTAL_SETTO’)) );
# }
# $_SESSION['coupon_discount'] = $GLOBALS['CURRENCY']->convert( $coupon_value );
#
# }
Please help me to solve this…
This is a great mod…. Great Job
November 25th, 2008 14:42
@Allan:
You have made changes to the code based on some errors I had in my first post. Please revise the changes. Some of the special characters were ‘mis-shown’ in the first version.
Also, as other have pointed out, the current hack is creating problems with product update. I’ll be posting a cleaner version soon.
November 26th, 2008 02:20
Thanks for your reply and looking forward for the release soon…
Great Job
December 1st, 2008 13:12
Hi,
I think this mod is great; is really what many have been hoping for VM. Do post a cleaner versioner soon, i can’t wait to use it for my shopping cart!
December 9th, 2008 16:01
@All:
I have cleaned up the code above.
Please not that the ps_database.php code is no longer applicable (this was causing the product update issue) and that the fields are not all text type, instead of the numeric type.
Though, this creates the problem of incorrect input possible, this is a solution nonetheless. Only if you screw up by putting wrong values can you make things to not work. And even in that case, it’ll not be a ‘loss’.
Hope that helps!
December 9th, 2008 23:50
Will this modification work on a site using Joomla 1.0?
December 10th, 2008 00:17
@Zenia:
So long as you are using virtuemart 1.1.x it could probably work out.
However, I have not tested it on a joomla 1.0 install so I can’t say anything with conviction.
You can let me know in case of any issues and I’d be glad to help!
January 24th, 2009 10:31
Hey I notice that when you add a product that is not on the coupon list and then add a product on the coupon list it gives the discount on the total order and not just the one product on the coupon list.
Is there a work around for this?
Thanks
January 24th, 2009 10:57
@Xyn1407:
You should be choosing specific discount when using the product specific % discount, at the coupon defining page. If you choose overall, with that particular product in cart, you will get discounton all product. This is a feature.
Hope that helps!
January 26th, 2009 23:09
Hi,
Thanks for your modification. I’ve followed every step several times, but can’t get it to work. Is it perhaps possible you provide a zip file with the modified code?
Thanks!
January 29th, 2009 09:11
@Combicart:
I haven’t put up zipped files because in that case if people had modifications in their existing files, they would be lost and could lead to other incompatibilities.
I can send you the files over email if I get time this weekend or If you let me know any specific issues I can help here.
January 29th, 2009 19:57
This is a lesson in frustration. Bits of the code are missing parts that make it break, instructions are half there (like “Modify the query variable ‘$q’ in this function to add the following variables” which means nothing to me) and the comments make me think the instructions are just WRONG in places. This could be a great addition to Virtuemart, but try as I might, I can’t implement it, and the farther I get in the comments, the worse it gets.
January 30th, 2009 00:48
Hello,
that looks very good.
Before I go through all the steps I have a question:
If we enable a product specific Coupon – is it possible that this coupon code is only used once per user?
Because I don’t want that a user can use the coupon code twice for one product.
All users can use this coupon code only once for a specific product. What that be possible?
Thanks for help and information.
Rob
January 30th, 2009 03:46
@Brian Peat:
This is not a paid support forum for a paid software modification that I made for you. I don’t care what you think of the mod or that it doesn’t work for you. If you are not able to use it, your tone of asking is not what makes me want to help you out.
@Robert:
That is possible. You’ll need to modify the database and write some additional code. It should not be a difficult task.
Use the contact form if you would like me to work on it.
Hope that helps!
February 5th, 2009 02:03
Thanks, great mod!
I just hacked it into my VM and it looks great.
I have one problem however, if I make a gift coupon for 10% only when the minimum order should be $ 20, it will use the original price.
So, when I have a product with a price of $25 and discount it to$19,95 it will accept a coupon made for mininum of $20 becuase that’s less than the original $25.
I’ll try to look into it tommorow to find where that might goes wrong.
Anyway, thanks for the nice mod, was looking for it.
February 5th, 2009 08:57
@Richard:
Yes, that’s how it’ll happen with the current scenario. I didn’t think about this situation when I wrote the modification
.
To overcome this, it’ll be just a single line change at the price comparison (in process_coupon function) to accomodate discounted prices.
Hope that helps!
February 5th, 2009 12:53
Thanks for your reply.
I’m not a VM devver, so it’s quite hard for me.
I thought it has something to dow with the line:
$price = $ps_product->get_adjusted_attribute_price($cart[$product_number]['product_id'], $cart[$product_number]['description']);
But when i google about it, the “get_adjusted_attribute_price” should allready be the discounted price.
To be specific : ALL of my products have 2 prices : a normale price and a discount price (thus, all of my products are ‘on sale’) bit it looks like it that your coupon system works with the original price, but that would only makes sense if you use “$ps_product->get_price”.
Any ideas?
February 5th, 2009 14:28
@Richard:
I was referring to the following line:
Now the d['total'] variable should has the total value of the order and my first impression was wrong. In your case, the total gets rounded off to $20, which causes the coupon to work. Just remove the round() and you should be set.
Hope that helps!
February 11th, 2009 01:24
Thanks for the latest information about the Round().
That did the trick!
It’s implemented in my shop right now, and works like a charm.
Thanks again for the mod and your help.
February 26th, 2009 19:24
Hi !
First of all great VM hack ! i was googling for it and your the only one i found with this solution.
but i have 1 question .. how can i make lets say 10 products 5 dollar cheaper with the same coupon code ? this coupon gives you 5 dollar off on all the vacuumcleaners… ?
i have it working (well have to add product_id) manual in my DB .. but hey
i tried making multiple coupons with the same code but thats it not allowed bij VM
Can you help me out please.
February 26th, 2009 19:26
Maybe you can help me i have email or msn ? herodiassatyr@hotmail.com
February 26th, 2009 23:04
@Webdesign:
That is possible with two methods. If you want me to work on it, it’ll cost you
If not, I can suggest you the methods and you can write the code yourself.
Let me know in either case. You can use the contact form if you choose the former option.
March 3rd, 2009 01:51
I keep getting error message, when i update the language file
Parse error: syntax error, unexpected T_STRING, expecting ‘)’ in /home/www/site.com/administrator/components/com_virtuemart/languages/common/english.php on line 575
i added what you said
“‘PHPSHOP_COUPON_USER_ID’ => ‘User ID’,
‘PHPSHOP_COUPON_MIN_VALUE’ => ‘Minimum order value’,
‘PHPSHOP_COUPON_DISC_TYPE’ => ‘Discount type’,
‘PHPSHOP_PRODUCT_DISCOUNT_TYPE_TIP’ =>; ‘Overall discount gives discount to all products while specific gives discount only to that specific product. Makes sense only if you are selecting percentage discount type’,
‘PHPSHOP_COUPON_DISC_OVERALL_TYPE’ => ‘Overall’,
‘PHPSHOP_COUPON_DISC_SPECIFIC_TYPE’ => ‘Specific’,
‘PHPSHOP_PRODUCT_ID_NOT_NUMBER’ => ‘Product ID is not a number’,
‘PHPSHOP_USER_ID_NOT_NUMBER’ => ‘User ID is not a number’,
‘PHPSHOP_MIN_VALUE_NOT_NUMBER’ => ‘Minimum value is not a number’,
‘PHPSHOP_PRODUCT_ID_NEEDED’ => ‘Product ID is required when selecting specific discount type’,
‘PHPSHOP_REMOVE_COUPON’ => ‘Remove’,”
March 3rd, 2009 10:25
@Ed:
In the line with ‘PHPSHOP_PRODUCT_DISCOUNT_TYPE_TIP’, there is an extra semicolon in the middle of the line. Just remove that and it will be fine.
Hope that helps!
March 5th, 2009 13:00
I did some homework, and I appreciate ANYONE who helps people out for free with coding as I was looking for this as well. I just wanted to point you in the direction of a COMPONENT I found that does all this in a easy format, with date options, per item, per user, per group, category, etc….
It patches the DB and can unpatch the db from the GUI, and gives a full component menu config for every option. I have no affiliation to these guys but it works pretty easily so I thought I would point it out.
http://www.purelifemarketing.com/
I personally need a Joomla Guru for hire. I think you might be the guy if you have time. I need tons of mods, custom modules, etc…. for many clients as I simply install and customize as it stands now with minimal PHP and SQL knowledge. Please email me regarding the possibility.
March 6th, 2009 10:43
@Ron F:
Thanks for the feedback! I had actually tried out that component before I developed this modification of my own. The only problem with the above is that it needs to run in legacy mode on joomla 1.5.
As far as functionality is concerned it is a much more professional implementation than mine with way too many features to be even compared to what I put together above
I’ll email you later about the work possibility as I am on a holiday right now
March 10th, 2009 07:47
alright just did everything one more error
tried to add the coupon. i get
“Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ‘)’ in /home/www/site.com/administrator/components/com_virtuemart/classes/ps_coupon.php on line 102″
line 102 start
‘product_id’ => $product_id != 0 ? $product_id : NULL,
‘user_id’ => $user_id != 0 ? $user_id : NULL,
‘min_value’ => $min_value != 0 ? $min_value : NULL,
what did i do wrong
March 10th, 2009 10:32
@Ed:
The error doesn’t necessarily have to be at 102. I think you might have missed a ‘,’ at the end of 101. Check again and let me know.
Also, if post a few lines more from the section to be sure.
Hope that helps!
March 10th, 2009 18:24
here it’s admin
}
$fields = array(
‘coupon_value’ => (float)$d['coupon_value'])
‘product_id’ => $product_id != 0 ? $product_id : NULL,
‘user_id’ => $user_id != 0 ? $user_id : NULL,
‘min_value’ => $min_value != 0 ? $min_value : NULL,
‘discount_type’ => strtolower($d['discount_type']) == ‘overall’ ? ‘overall’ : ‘specific’,
‘coupon_code’ => vmGet($d,’coupon_code’),
‘percent_or_total’ => strtolower($d['percent_or_total']) == ‘percent’ ? ‘percent’ : ‘total’,
‘coupon_type’ => strtolower($d['coupon_type']) == ‘gift’ ? ‘gift’ : ‘permanent’,
‘coupon_value’ => (float)$d['coupon_value']
now i get
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/www/site.com/administrator/components/com_virtuemart/classes/ps_coupon.php on line 102
March 17th, 2009 19:35
Great Mod, its implemented in my shop, and works fine.
But i have a little problem, if i update the coupon in the backoffice, the Minimum value dont update the jos_vm_coupons-table, only a NULL-value was updatet in the min_value field.
Whats the Problem?
March 17th, 2009 19:51
My post Number 52;
the problem is fixed, my update_coupon-function in ps_coupon.php wasnt correct.
Thank you for the great modification.
March 28th, 2009 22:43
how much to have you install this, i went through the steps but came up with an error
Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in /administrator/components/com_virtuemart/classes/ps_coupon.php on line 80
March 28th, 2009 23:41
actually i was able to fix that problem (post 54) but now when i add products to cart the coupon stays even if customer lowers the cart value.
example: i have coupon for $20off $100cart, well it works when they load up the cart to $100 but when they change the quantity of an item and take cart to lets say $70 they still get the $20off, what should i do to fix this?
March 29th, 2009 10:02
@Ink:
have you made the modification to administrator/components/com_virtuemart/classes/ps_cart.php ?
From what you are reporting, it seems to me you have not as this mod is precisely to facilitate the functionality in question.
Hope that helps!
April 8th, 2009 02:52
Great feature and obviously one that VM should have added as a part of their core. Hopefully they will see your wisdom and do this. Many thanks!
April 8th, 2009 03:55
I found a strange problem (BUG) with the VM code for saving a coupon that you might want to know. If you are editing a coupon and hit the “Apply” button multiple times the second time will generate an error saying “Error: That coupon code already exists. Please try again.” The easy fix is right around line 170 of the ps_coupon.php file in the update_coupon() function. Comment out the line “$_REQUEST['coupon_id'] = $coupon_db->last_insert_id();” so that the $coupon_id variable is not wiped out. Probably not a big deal but something our clients might complain about.
Hope this helps someone!
April 12th, 2009 09:34
@KOA Consulting:
Thanks for the good words
. It has actually been already accepted by the VM team and it would hopefully make it into the 1.2 release.
I haven’t checked your fix, but I know the issue that you are talking about. If it works, it is indeed a very nice fix for an issue that can be worked around, but should not be there in the first place!
You should post it in the QA section of the vm forums so that it can be fixed in the next update!
April 14th, 2009 22:57
Thanks a bunch, this thing works like a charm. This is a very exciting feature.
I was wondering about the comment you made to ‘webdesign’ on feb 26th. Is it possible to make a discount apply to multiple products?
A pointer in the right direction would be greatly appriciated!
April 15th, 2009 09:13
@Martin:
You will need to modify the form input file (coupon.coupon_form.php) to take ids of multiple products.
Then, you need to modify the ps_coupon file and change the checking of one product to handle multiple products.
Hope that helps!
May 2nd, 2009 02:03
I’m interested in applying this mod to VM 1.1.2 and J1.5 but I see that the original code listed above needs some changes. Can you please share with me the updated files? I have some discounted items on my shop and I need to tie the discount codes to particular products or my shop won’t work. Your modification is a timely addition to a bit of missing Virtuemart!
May 3rd, 2009 21:49
This modification works, no question about it. It is a bit tricky to apply for a newbie as it is not explained as fully as it could be. Also there are one or two bug-ettes in the source code above that will generate errors. If you want a solution to product specific coupon codes then this WILL work with a bit of work. Get a friendly VM enthusiast to implement this for you and it will give you the functionality you need. My thanks to the developer. You saved my skin. There’s a beer waiting for you…
May 4th, 2009 06:45
Hi
Thanks a lot for this great work and for taking time to respond to questions etc etc.
I checked http://www.purelifemarketing.com/ mod first but it seems to be having lot of errors which I cannot fix. I am running VM 1.1.3
Then I implemented your hack. There seems to be no errors now but when I try to apply coupons (for registered customers-after login) I get the massage “Coupon code not found”- note: this does not come as coding error.
I must be doing something wrong.
a) I am little confused about terminology-product ID and customer ID. would appreciate if u could clarify
Is product id same as sku? (when you define sku as 0001 then the hack change it to 1 when saved).
What is the customer id (I guess it is not username? is it the Customer Number / ID: in shopper information tab in VM user area?) -e.g., user id here is 3243549ccc5897e824 and when saved in new coupon set up it changes to 324549? when viewed again?.
b) could u also please explain whether we could use the customer ID, product ID and minimum value independently? that is would it work if I create a coupon say abc1/ type -%/ type-gift/ Value=5/product id=sku001 (but without specifying min val and customer ID). similarly would it also work if I specify customer ID but not product ID or min value.?
in creating the fields from phpmyadmin this is how I did it- say for the field “min_value: — Type: “Text”, Length/Values (nothing is entered), Default: NULL, collation (nothing entered but it has automatically changed to iltf8_general_ci), Null: ticked, A_I (not ticked), comments (nothing)
please advise. many thanks
sanjeewasam
May 10th, 2009 06:56
Hi,
I salute you for the great work you have done with VM and your contribution to joomla and VM.
Is it possible to change your hack to create create coupons that could be applied for particular user group? your current hack seems to be working for individual user which is good but difficult administer.
new User groups are created via access control systems such as JACL pro etc.
This modification would help all people with membership systems with VM based shop front where they would like to give discounts to each member group.
I may be able to afford a small payment for your advise. However, cannot afford much as I am a recently laid off accountant now trying to make out of something on line with little success yet. I have checked your existing hack and got it working for products but I cannot find the userID’s.
thanks in advance.
sanjeewa
May 10th, 2009 17:53
@sanjeewasam:
The product ids are visible in the product list page of the VM administration panel. Same goes for the user ids.
If you want to use a group of users, you can change the user id checking to user group checking for the functionality you are seeking.
Hope that helps!
May 17th, 2009 19:14
Before I try to add these codes to my site I just wanted to ask have all the fixes been applied to the code and instructions you have listed above? I noticed quite a few bug fixes and changes in all the comments.
Also would this allow me to add a coupon to just 1 single product rather than the current VM coupon which is for any product?
Thank you for your work and congrats on grabbing VMs attention with your code!
May 18th, 2009 14:20
@BR29:
There should be no issues right now, although there was some trouble with the syntax highlighter plugin which may lead to some errors.
If you do get an error, please let me know!
May 31st, 2009 04:10
Hello,
I guess this diesn `t work with joomla 1.5.10 & virtuemart 1.1.3?
In the very first step I am trying to paste the code into coupon.coupon_form.php before the final and i am getting a markup error in dreamweaver “marked invalid because of a duplicate attribute”
invalid markup – ‘ in /home/…/administrator/components/com_virtuemart/html/coupon.coupon_form.php on line 184″
line 184 in this case is the first line of modified code
“<input class=”inputbox” name=”product_id” type=”text” value=”sp(” />” /> ”
any suggestions would be greatly appreciated
May 31st, 2009 12:09
@John:
Actually some text has gone missing in the above post due to some changes in the syntax highlighter plugin.
I’ll correct the code above and let you know in a couple of days.
To address your concern, the modification works as-is on the configuration you asked for.
Hope that helps!
June 2nd, 2009 07:40
Sorry if I`m being dense – are you saying there is some code missing from the modification I was trying to do, and you are going to be updating the code shortly?
June 2nd, 2009 10:07
@John:
Just from the first piece of modification (which is where you are getting the error), yes. I’ll update it latest by the weekend, so just check in again around that time!
June 7th, 2009 22:57
@John:
You should now be able to use the above code (and hopefully no more issues are remaining).
If things just don’t work, I’ll probably make an attachment of the files later sometime. The only problem with using that would be all existing modifications to your corresponding files would be lost.
Let me know if things work out!
June 10th, 2009 03:50
I`m not sure what you meant by ‘make sure you add a ‘,’ to the last value’ – I tried adding a ‘,’ after ‘specific’
” ‘discount_type’ => strtolower($d['discount_type']) == ‘overall’ ? ‘overall’ : ‘specific’ , ”
but that`s obviously not it
I`m getting an error
“Parse error: syntax error, unexpected T_STRING, expecting ‘,’ or ‘;’ in /root/administrator/components/com_virtuemart/html/coupon.coupon_form.php on line 202″
June 10th, 2009 09:26
@John:
The ‘,’ is to be added after the following line:
‘coupon_value’ => (float)$d['coupon_value']
as this is the last line in the existing array and you are adding more values to the same. Do not add a ‘,’ to the end of the line you mentioned above.
Hope that helps!
June 10th, 2009 20:57
I`m a bit out of my element here, I do appreciate all your help, I changed the code but i`m still getting the same message.
I guess i`ll just monitor this thread, and try again when you `ve had a chance to upload zip files.
Thanks again for your patience
June 25th, 2009 10:32
Hi there:
I’m eager to try this code, but I got a parsing error when I pasted the primary code into coupon.coupon_form.php. I narrored down the error to the second in the final row of the code
Here’s what the error looked like:
Parse error: syntax error, unexpected T_STRING, expecting ‘,’ or ‘;’ in /home/public_html/administrator/components/com_virtuemart/html/coupon.coupon_form.php on line 112
Would love to get started on this!!
Thanks
June 25th, 2009 11:07
@Pete:
Could you post your code here near the line in question ?
June 25th, 2009 17:37
I posted the entire coupon.coupon_form.php file. The only change to the file was the insertion of your code above. Thanks for looking!
query($q);
$db->next_record();
if( $db->f(“coupon_type”)==”gift”) {
$selected[0] = “selected=\”selected\”";
$selected[1] = “”;
}
else {
$selected[1] = “selected=\”selected\”";
$selected[0] = “”;
}
$title = $VM_LANG->_(‘PHPSHOP_COUPON_EDIT_HEADER’);
}
else {
$selected[0] = “selected=\”selected\”";
$selected[1] = “”;
$title = $VM_LANG->_(‘PHPSHOP_COUPON_NEW_HEADER’);
}
//First create the object and let it print a form heading
$formObj = &new formFactory( $title );
//Then Start the form
$formObj->startForm();
?>
_(‘PHPSHOP_COUPON_COUPON_HEADER’) ?>:
<input type="text" class="inputbox" name="coupon_code" value="sp(“coupon_code”) ?>” />
_(‘PHPSHOP_COUPON_PERCENT_TOTAL’) ?>:
<input type="radio" class="inputbox" name="percent_or_total" value="percent" sf(“percent_or_total”)==’percent’ || empty($coupon_id)) echo “checked=\”checked\”"; ?> />
_(‘PHPSHOP_COUPON_PERCENT’) ?>
_(‘PHPSHOP_PRODUCT_DISCOUNT_ISPERCENT_TIP’) ); ?>
<input type="radio" class="inputbox" name="percent_or_total" value="total" sf(“percent_or_total”)==’total’) echo “checked=\”checked\”"; ?> />
_(‘PHPSHOP_COUPON_TOTAL’) ?>
_(‘PHPSHOP_COUPON_TYPE’) ?>:
<option value="gift" >
_(‘PHPSHOP_COUPON_TYPE_GIFT’) ?>
<option value="permanent" >
_(‘PHPSHOP_COUPON_TYPE_PERMANENT’) ?>
_(‘PHPSHOP_COUPON_TYPE_TOOLTIP’) ); ?>
_(‘PHPSHOP_COUPON_VALUE’) ?>:
<input type="text" class="inputbox" name="coupon_value" value="sp(“coupon_value”); ?>” />
_(‘PHPSHOP_COUPON_PRODUCT_ID’) ?>:
<input type="text" class="inputbox" name="product_id" value="sp(“product_id”); ?>” />
_(‘PHPSHOP_COUPON_USER_ID’) ?>:
<input type="text" class="inputbox" name="user_id" value="sp(“user_id”); ?>” />
_(‘PHPSHOP_COUPON_MIN_VALUE’) ?>:
<input type="text" class="inputbox" name="min_value" value="sp(“min_value”); ?>” />
_(‘PHPSHOP_COUPON_DISC_TYPE’) ?>:
<input type="radio" class="inputbox" name="discount_type" value="overall" sf(“discount_type”)==’overall’ || empty($coupon_id)) echo “checked=”checked”"; ?> /> _(‘PHPSHOP_COUPON_DISC_OVERALL_TYPE’) ?> _(‘PHPSHOP_PRODUCT_DISCOUNT_TYPE_TIP’) ); ?> <input type="radio" class="inputbox" name="discount_type" value="specific" sf(“discount_type”)==’specific’) echo “checked=”checked”"; ?> /> _(‘PHPSHOP_COUPON_DISC_SPECIFIC_TYPE’) ?>
hiddenField( ‘coupon_id’, $coupon_id );
// Write your form with mixed tags and text fields
// and finally close the form:
$formObj->finishForm( $funcname, $modulename.’.coupon_list’, $option );
?>
June 28th, 2009 09:06
@Pete:
What you have posted seems like completely fucked up version of coupon_form.php file.
Please make the above modifications in a fresh file and try again. There are a lot of missing statements, which is why you are getting errors.
Hope that helps!
June 29th, 2009 02:25
@admin:
Ok – for some reason, the entire code did not paste properly. Here it is again and I did try it again, but still getting the parsing error. the error appears to be coming from the second cell
Thanks
——————————————————
query($q);
$db->next_record();
if( $db->f(“coupon_type”)==”gift”) {
$selected[0] = “selected=\”selected\”";
$selected[1] = “”;
}
else {
$selected[1] = “selected=\”selected\”";
$selected[0] = “”;
}
$title = $VM_LANG->_(‘PHPSHOP_COUPON_EDIT_HEADER’);
}
else {
$selected[0] = “selected=\”selected\”";
$selected[1] = “”;
$title = $VM_LANG->_(‘PHPSHOP_COUPON_NEW_HEADER’);
}
//First create the object and let it print a form heading
$formObj = &new formFactory( $title );
//Then Start the form
$formObj->startForm();
?>
_(‘PHPSHOP_COUPON_COUPON_HEADER’) ?>:
<input type="text" class="inputbox" name="coupon_code" value="sp(“coupon_code”) ?>” />
_(‘PHPSHOP_COUPON_PERCENT_TOTAL’) ?>:
<input type="radio" class="inputbox" name="percent_or_total" value="percent" sf(“percent_or_total”)==’percent’ || empty($coupon_id)) echo “checked=\”checked\”"; ?> />
_(‘PHPSHOP_COUPON_PERCENT’) ?>
_(‘PHPSHOP_PRODUCT_DISCOUNT_ISPERCENT_TIP’) ); ?>
<input type="radio" class="inputbox" name="percent_or_total" value="total" sf(“percent_or_total”)==’total’) echo “checked=\”checked\”"; ?> />
_(‘PHPSHOP_COUPON_TOTAL’) ?>
_(‘PHPSHOP_COUPON_TYPE’) ?>:
<option value="gift" >
_(‘PHPSHOP_COUPON_TYPE_GIFT’) ?>
<option value="permanent" >
_(‘PHPSHOP_COUPON_TYPE_PERMANENT’) ?>
_(‘PHPSHOP_COUPON_TYPE_TOOLTIP’) ); ?>
_(‘PHPSHOP_COUPON_VALUE’) ?>:
<input type="text" class="inputbox" name="coupon_value" value="sp(“coupon_value”); ?>” />
_(‘PHPSHOP_COUPON_PRODUCT_ID’) ?>:
<input type="text" class="inputbox" name="product_id" value="sp(“product_id”); ?>” />
_(‘PHPSHOP_COUPON_USER_ID’) ?>:
<input type="text" class="inputbox" name="user_id" value="sp(“user_id”); ?>” />
_(‘PHPSHOP_COUPON_MIN_VALUE’) ?>:
<input type="text" class="inputbox" name="min_value" value="sp(“min_value”); ?>” />
_(‘PHPSHOP_COUPON_DISC_TYPE’) ?>:
<input type="radio" class="inputbox" name="discount_type" value="overall" sf(“discount_type”)==’overall’ || empty($coupon_id)) echo “checked=”checked”"; ?> /> _(‘PHPSHOP_COUPON_DISC_OVERALL_TYPE’) ?> _(‘PHPSHOP_PRODUCT_DISCOUNT_TYPE_TIP’) ); ?> <input type="radio" class="inputbox" name="discount_type" value="specific" sf(“discount_type”)==’specific’) echo “checked=”checked”"; ?> /> _(‘PHPSHOP_COUPON_DISC_SPECIFIC_TYPE’) ?>
hiddenField( ‘coupon_id’, $coupon_id );
// Write your form with mixed tags and text fields
// and finally close the form:
$formObj->finishForm( $funcname, $modulename.’.coupon_list’, $option );
?>
June 29th, 2009 02:28
@admin
weird, it’s truncating everything before the word “query” when I post. I sent you a message via your contact link.
Thanks
June 29th, 2009 08:41
@Pete:
post your link at a code sharing site like http://pastebin.ca/ and post the link here.
I think the code is getting truncated for security
June 30th, 2009 01:17
link to code is
http://pastebin.ca/1478807
Hope this helps…
July 5th, 2009 22:50
@Pete:
I have fixed the code at the URL you have pasted. Copy it at your install and it should be fine!
July 8th, 2009 10:31
I checked out the link to the file and found it giving me the same error. I believe the error is
in this line:
sf(“discount_type”)==’specific’) echo “checked=”checked”"; ?> />
it should be:
sf(“discount_type”)==’specific’) echo “checked=\”checked\”"; ?> />
July 8th, 2009 11:35
@cherbel:
correct, that was the issue and I thought I fixed it in the pastebin link, but it still shows the old one.
In any case, I have updated the post above with this!
July 17th, 2009 01:07
You’re a genius. I’m a PHP novice but followed your instructions and got it to work. Is it possible to use the same coupon code for multiple items?
July 17th, 2009 02:04
Hi – I’m running VM 1.1.3 and Joomla 1.5.12. Does anyone know of a modification that will allow for a coupon to be sent on registration? Thx in advance for any help
July 17th, 2009 10:50
@Melissa:
Yes, it is possible. If you need to do it for categories, just change the code for checking category id instead of product id.
If you specifically need multiple products, then you just need to add more fields in the coupon form and check for them in the coupon processing.
@paul:
Are you looking for one-time coupon generation for new registrants ? That is possible.
If it’s a generic coupon code that you want to send to all people when they register, then you need to edit the emailing template.
Hope that helps!
July 22nd, 2009 11:28
@admin:
Thanks for the hard work on this. It is very valuable to my site, as many others.
All appeared to update well, with the exception of one problem I am having. (btw I am using Joomla 1.5.11 and VM 1.1.3) Now when I attempt to update a coupon, or save a new coupon, I get a blank screen. I am only able to access my Joomla Admin panel by pressing Back, which then shows me the coupons are neither saved or updated. I have narrowed this down to a problem with ps_coupon.php between lines 256 and 293. I have uploaded this file to http://pastebin.ca/1502926.
Any help would be much appreciated. Thanks again.
July 22nd, 2009 15:22
@jonathan:
I have not checked your code yet, but before that, have you made changes to the sql table ?
If not, then the coupon addition / updation will not work.
Hope that helps!
July 22nd, 2009 19:33
@admin
Thanks for the input. I did change the sql table as instructed, with all new fields ‘text’ type and null defaults. In fact I am able to save the coupons with the new variable so long as that code as mentioned above is not updated. The changes are successfully updated in the SQL database. Only once the ps_coupon.php file (following “if ($coupon_db->f(‘percent_or_total’) == ‘percent’” around line 256) is updated, will the coupons not save to the database.
Hope this makes sense. Thanks
July 30th, 2009 16:45
Hello, i tried to do this to my website, but i got stuck after
1.’coupon_value’ => (float)$d['coupon_value']
since my english is not well.
My question is, does anyone hav the right files for Joomla 1.5.12 and vm 1.1.3? So i can upload them to my site.
September 9th, 2009 19:46
Hi,
I am quite terrible at this but how to I access phpmyadmin or find the sql syntax to do this step?
Thanks.
–
If you have phpmyadmin, then just add the following fields to ‘jos_vm_coupons’ table (jos_vm prefix might be different for you if you chose a different prefix while installing).
product_id, user_id, min_value, discount_type
All of the fields should be ‘text’ type. All of them should have default value as null and should be nullable. If you do not have phpmyadmin, search for sql syntax and add these tables as specified above.
September 14th, 2009 23:23
This addition to Virtuemart stunned me!! it works brilliant! Most modules are harder to configure and install. Thanx!
September 17th, 2009 21:32
Hi Admin.
I think I’ve managed everything. But I haven’t got a clue when it comes to
Modify the query variable ‘$q’ in this function to add the following variables
product_id, user_id, min_value, discount_type
Any advice you can give me would be very much appreciated.
September 18th, 2009 00:26
Hey I got it all working now. If anyone else gets stuck you need line 37. It should end up like this
$q = “SELECT coupon_code FROM #__{vm}_coupons WHERE coupon_code = ‘”.$coupon_db->getEscaped($d['coupon_code','product_id','user_id','min_value','discount_type']).”‘ “;
————–
ADMIN.. can you add more than one product to the specific product box?
I was thinking along the lines of buy these three products and from our virtueproduct range and recieve our super virtueproduct discount
October 2nd, 2009 19:58
I have created the same functionality for Joomla 1.0.15 and Virtuemart 1.0.15 so if you are interested in picking up a version for VM 1.0 I will have it ready soon for downloading. The changes are very similar to the above. The changes will be downloadable from the download section at http://www.lightquick.co.uk
October 2nd, 2009 22:17
I have parcelled it up so that the Virtuemart 1.0.15 version of this hack can be found here: http://lightquick.co.uk/jdownloads/virtuemart-1.0.15-product-item-specific-coupon-codes.html
October 2nd, 2009 22:19
I have also bundled up the VM 1.1.2 version here for download which should make it easier for anyone to install:
http://lightquick.co.uk/virtuemart-downloads/virtuemart-1.1.2-product-item-specific-coupon-codes.html
October 2nd, 2009 22:22
Finally, do you mind if I put this on the JED? this is very useful functionality and now that there are versions for both VM 1.0.15 and VM 1.1 then it would seem wise to place them where they can do most good. I am always happy to help create a new VM 1.0 extension as the majority of my shops are VM 1.0. Can you please send me the author’s full name so that I can credit you in the download.
October 2nd, 2009 23:39
@Yereverluvinunclebert:
I haven’t seen the modifications, but great work! I don’t mind you posting the modifications to JED, by all means please do go ahead! You can use ‘sobers_2002′ as the author to credit for the work.
The modifications have actually been made upstream and will hopefully reflect in VM 1.2, but till then I suppose this will be a great help for people!
@Others:
Please use the above code files posted and let me know if they work out fine. If so, I’ll edit the original post and provide the links give by Yereverluvinunclebert!
October 3rd, 2009 16:59
If anyone tries either of the versions I have hacked and finds any issues I’ll be happy to incorporate your changes in the download section on the lightquick site. Just visit the contact form and send me an email or post the changes here.
Yereverluvinunclebert
October 4th, 2009 12:30
Hi,
Could the VM coupon system be modified to something similar to the couponcabin site.
Rgds
October 5th, 2009 08:48
@Bapajan:
Can you be a little more precise ? couponcabin is a site that posts coupon codes for use on other websites. I am not sure how this system is related to that.
October 9th, 2009 19:17
I have submitted the hack to the JED and it is awaiting review. The poor chap has 320 extensions to review before this one gets to the front of the queue…
November 2nd, 2009 21:18
Hi,
Great mod, but exists 1 big problem, its the same of Richard :” if I make a gift coupon for 10% only when the minimum order should be $ 20, it will use the original price.
So, when I have a product with a price of $25 and discount it to$19,95 it will accept a coupon made for mininum of $20 becuase that’s less than the original $25.”
its a problem with quantiy…
How can i fix it??
Thanks a lot!
November 11th, 2009 14:18
Just got word back from the JED that they no longer accept hacks and instead the change has to be rolled up into a standard Joomla installation. So I’ll have to see what can be done to create an installation. So this hack is not on the JED just yet.
November 11th, 2009 14:22
I have just noticed in the VM 1.0.15 conversion
that selecting a coupon with a % discount causes the following error:
Fatal error: Call to a member function convert() on a non-object in /home/carstuff/public_html/administrator/components/com_virtuemart/classes/ps_coupon.php on line 291
Line 291 is
$product_price = $GLOBALS['CURRENCY']->convert( $price['product_price'], $price['product_currency'] );
I am not a PHP programmer more of a hacker and this problem has me stumped, can I send you the VM1.0.15 code for you to have a quick look?
November 11th, 2009 22:16
@Yereverluvinunclebert:
The modification has been made in upstream for 1.2 release, so I guess it isn’t really worth putting in the effort to get it integrated via JED. If you want to make the installer to get it into JED, by all means please do so!
You can post the code on pastebin and I’ll take a look, but I doubt if I’ll be able to help much as things were quite different structurally in 1.0.x.
November 12th, 2009 18:58
It’s on pastebin now
http://pastebin.com/m15d9a10a
November 15th, 2009 18:59
@Yereverluvinunclebert:
The file that you posted doesn’t have $GLOBALS.. like you posted and instead $_REQUEST..
The error is basically saying that the object in question is not an object or doesn’t exist. So just see the remaining file and you’ll see what to call the convert() function on.
Hope that helps!
November 26th, 2009 04:24
hmmmm, actually I had already tried that but I get the same message…
November 28th, 2009 04:10
Hello,
my problem is that the discount applies to the price of the product without the rebate I applied in that product, so that 10% is not correct.
Please help.
thanks
November 30th, 2009 23:58
@admin
Hello there and firstly let me say what a wonderful little addition this is for the VM coupon. They should be jumping all over you to get this incorporated as standard!
I have got it up and running on VM 1.1.4 and Joomla! 1.5.9 and LOVE IT… but when you insert coupon code (like one of the guys above!) for £10 discount, it shows the right discount amount, but subtracts £20 from the total. Can you tell me what’s gone wrong?
Again… I love the work you’ve done here, and would be happy to make an (embarrassingly) small donation (a fiver?) if you could help me out with this.
Cheers, darlin’ !
December 1st, 2009 00:19
P.S. – if you want a looksee, go onto the website and use coupon code XMAS09 for any item valued over £100.
Cheers!
January 3rd, 2010 04:37
This may be a general vm coupon issue – I had the same problem before using this hack and found the solution here which worked for me:
http://forum.virtuemart.net/index.php?topic=62421.0
January 3rd, 2010 08:29
@George:
The issue resolved is indeed a generic vm coupon issue. Thanks for linking it here!
I’ll update the post to include this solution.
January 21st, 2010 17:10
Great Thanks! It’s really a nice mod. As I can’t really follow the instruction above fully, I downloaded the file provided by Yereverluvinunclebert (Post 101), performed the rest on the database and it worked on my VM1.1.4.
However, just one thing as I found out the same problem encountered by Ink (Post 55):
“example: i have coupon for $20off $100cart, well it works when they load up the cart to $100 but when they change the quantity of an item and take cart to lets say $70 they still get the $20off, what should i do to fix this?”
I have double checked the “ps_cart.php” (Post 56), it’s already been modified. Any idea what else could have caused it?
January 25th, 2010 13:04
Thanks – this works very very well!
January 28th, 2010 18:00
@George:
Just checked in to see if anyone had provided a solution for this and there you were… thanks loads!
Will try out the fix now…
Cheers
February 4th, 2010 22:37
Hi, anyone managed to modify this to add functionality to give discount per category?
February 6th, 2010 02:57
When I set a 10% overall coupon on a minimum order of $50, with no product or user id set, the order is processed successfully with the correct amounts but the coupon record in jos_vm_coupons is deleted when the order is placed by the first user to use the coupon. What I want is a one time coupon for any user, not one time for all users.
Right now if Joe uses a coupon and then Sam orders, Sam cannot get a discount. What I want is Joe to get one coupon use and Sam to get one coupon use and no more-i.e once per user. How do I do that?
How does this work when the users do not register-my site allows users to order without registering?
Also, it would be great to have a date range for this.
TIA,
Steve
February 6th, 2010 03:00
Oh and this was a gift coupon.
Steve
February 11th, 2010 18:49
great work.. installed without any problems ^^ ..
1 question. if i create a 5eur coupon “XFEGW5″ for minimum value of 15eur and my customer put that in with less then 15eur, he get the notice that die coupon didnt found. As soon as he reach the minimum value the coupon above work fine. So what i want to do is, that the coupon system checks the code and then seperate between valid code (if it exists) and the requirement of an existing one.. u know what i mean.. the idea behind this is to send out 500 peaces of examples with 6-digit code on the backside, so the customer can put that in.. understand? (sry for my bad english ;P)
February 12th, 2010 12:30
hi, thanks for the great work.
One Problem still there. When i create a coupon with minimum oder value of 15eur, the message for orders under this value when putting in the coupon is, that there is no coupon. but i want that as this.. “the minimum order value for this coupon is 15EUR!” .. and when the coupon is wrong then” this coupon doesnt exist”
btw: i just wrote yesterday a message but it was deletet?!
February 16th, 2010 08:19
@Serce:
In the process_coupon function, just add another type of message if the value is less than required and add that text type in the language file so that the message is displayed when user has order value less than reqd.
Hope that helps!
February 18th, 2010 14:03
This really does look likie a great package and normally i would dive on in and have a go, i have even backed up my site in preparation but bottling it a little !
Am i right in assuming that the file changes are generic to everyone? that there arent specific domain related edits ?
If this is the case would someone be able to post a link to the tried and tested working files so they could be added to everyones site or is this not possible ?
hope it is because i am sorely tempted to have a go but usually regret it afterwards !!
cheers
March 7th, 2010 00:49
I’m working on installing this hack, as well as doing the hack for a category. My issue is this: For easy of end user (which I am not), one suggestion would be to display a list of Products and/or Categories so the end user doesn’t have to go find the Product_id/Category_id. Its something to think about?
March 7th, 2010 04:04
Also, being able to add multiple products and multiple categories would be extremely useful… I’m working on using a list in the category_id and product_id but I’m having some issues getting the list to stick when the form submits.
For instance:
I insert ’1′ and var_dump shows 1.
I insert ’12′ and var_dump shows 12.
I insert ’1,2′ and var_dump shows 1 again without the comma and 2.
Interesting. I will display the code once I’ve figured it out.
March 7th, 2010 05:06
I’ve figured out the output issue. If the field has _id at the end, it will return only an integer. You have to remove the underscore to be able to put a value list.
March 9th, 2010 01:00
Hello,
I have the same question as Joe – I need to have the coupon usable only once per user.
(including only 1 discount per order – eg if the customer orders 5 of the product they should receive the discount once, not on 5x)
is this possible?
March 17th, 2010 06:28
Hi.
I uploaded the file and tried it.
The coupon is for discount type is for all.
I have this message.
No valid database connection You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ” at line 1 SQL=SELECT product_parent_id FROM jos_vm_product WHERE product_id=
Name
This is the coupon info from DB.
XIWJFOA percent gift 99.00 overall
July 2nd, 2010 16:32
HI,
Can the voucher code be applied to multiple products?
Is this possible?
Thanks