Author Topic: HACK - 'Remove' button for coupons  (Read 7642 times)

themikeman

  • Beginner
  • *
  • Posts: 10
HACK - 'Remove' button for coupons
« on: March 13, 2010, 06:57:13 am »
This hack is applied against VM 1.1.4

First, define a new function in Virtuemart for us to use. In the Virtuemart backend, go to the Admin tab, click on "List Modules", find the "Coupon" module in the list, and click on the "Function List" link. You should see the existing four functions that the Coupon module uses.

Click the 'New' button at the top right, and add a new function with the following properties:

Function Name - couponCartRemove
Class Name - ps_coupon
Class Method - process_coupon_code_remove
Function Perms: select only 'no restriction'
Description: Removes a Coupon from the Cart

Then in /administrator/componenets/com_virtuemart/classes/ps_coupon.php, under  the existing function "process_coupon_code" add the function below. All we are doing here is un-setting the session variables for the coupon. The existing code for the cart will take care of the rest.

Code: [Select]
function process_coupon_code_remove( $d ) {
/* remove session variables marking the order as using a coupon */
$_SESSION['coupon_redeemed'] = false;
unset($_SESSION['coupon_id']);
unset($_SESSION['coupon_code']);
unset($_SESSION['coupon_type']);
}

Now open /administrator/components/com_virtuemart/html/basket.php and find the block for processing the display of the coupon discount (should be around line 259). What we need to do is add a button alongside the coupon display to remove it. Obviously feel free to replace the submit button with a custom image button or give it a custom class so you can style it.

Replace the block of code that at ~line 259 with the following:

Code: [Select]
/* COUPON DISCOUNT */
if( PSHOP_COUPONS_ENABLE=='1' && @$_SESSION['coupon_redeemed']=="1" && PAYMENT_DISCOUNT_BEFORE != '1') {
$discount_after=true;
$total -= $_SESSION['coupon_discount'];
$coupon_display = "- ".$GLOBALS['CURRENCY_DISPLAY']->getFullValue( $_SESSION['coupon_discount'] );
$coupon_display .= '<form action="'.$action_url.'" method="post" name="remove">
<input type="hidden" name="option" value="com_virtuemart" />
<input type="hidden" name="page" value="'. $page .'" />
<input type="hidden" name="Itemid" value="'. $sess->getShopItemid() .'" />
<input type="hidden" name="func" value="couponCartRemove" />
<input type="hidden" name="product_id" value="'. $_SESSION['cart'][$i]["product_id"] .'" />
<input type="hidden" name="description" value="'. $cart[$i]["description"].'" />
<input type="submit" name="Remove" title="Remove" value="Remove" />
  </form>';

}
else if( PSHOP_COUPONS_ENABLE=='1' && @$_SESSION['coupon_redeemed']=="1" && PAYMENT_DISCOUNT_BEFORE == '1') {
$discount_after=false;
$total -= $_SESSION['coupon_discount'];
$total -= ($_SESSION['coupon_discount'] * $my_taxrate);
$tax_total += ($_SESSION['coupon_discount'] * $my_taxrate);
$coupon_display = "- ".$GLOBALS['CURRENCY_DISPLAY']->getFullValue( $_SESSION['coupon_discount'] );
$coupon_display .= '<form action="'.$action_url.'" method="post" name="remove">
<input type="hidden" name="option" value="com_virtuemart" />
<input type="hidden" name="page" value="'. $page .'" />
<input type="hidden" name="Itemid" value="'. $sess->getShopItemid() .'" />
<input type="hidden" name="func" value="couponCartRemove" />
<input type="hidden" name="product_id" value="'. $_SESSION['cart'][$i]["product_id"] .'" />
<input type="hidden" name="description" value="'. $cart[$i]["description"].'" />
<input type="submit" name="Remove" title="Remove" value="Remove" />
</form>';

}


If you have any issues let me know and I'll see what I can do. This is something so simple that it really should be included in the next release of VM, making it that much more of an awesome, open source e-commerce software!

Also, if anything in here poses a security risk or anything like that, let me know, because my knowledge of Virtuemart is fairly limited still.

SodaJim

  • Jr. Member
  • **
  • Posts: 113
Re: HACK - 'Remove' button for coupons
« Reply #1 on: April 12, 2010, 19:19:19 pm »
While searching for implementing a coupon structure/system, I noticed several issues; however, one related to this thread prompted me to inquire...

Quote
In the Virtuemart backend, go to the Admin tab, click on "List Modules", find the "Coupon" module in the list, and click on the "Function List" link. You should see the existing four functions that the Coupon module uses.

Is there any detailed documentation about these settings and these proper uses...?
My installation doesn't have "shopper" permissions for "couponDelete" which, to me, means an end user, shopper if you will, cannot allow a coupon to be removed...

Just chiming in for the benefit of research so I can attempt to implement some form of coupon system that is beneficial to my users.

Any direction is appreciated,
Jim

RCheesley

  • Jr. Member
  • **
  • Posts: 81
Re: HACK - 'Remove' button for coupons
« Reply #2 on: October 01, 2010, 18:06:57 pm »
To use this in 1.1.15 the function needs to change from process_coupon_code_remove to coupon_code_remove otherwise it works just fine - thank you very much :)

vtecvsix

  • Beginner
  • *
  • Posts: 1
Re: HACK - 'Remove' button for coupons
« Reply #3 on: March 03, 2011, 22:38:05 pm »
It works fine with 1.1.15 but when there is tax, the tax does not get recalculated. It keep the tax amount after the discounted price.  Any fix for this one?  Thanks.

zhan528

  • Beginner
  • *
  • Posts: 2
Re: HACK - 'Remove' button for coupons
« Reply #4 on: June 19, 2012, 00:00:33 am »
I am trying to use the scripts in VM 1.5.23. However, after I have put the script into respective php files, when checking out, the calculation of coupon is all right, but the end value for customer to pay was way off. Any idea?

zhan528

  • Beginner
  • *
  • Posts: 2
Re: HACK - 'Remove' button for coupons
« Reply #5 on: June 27, 2012, 00:26:55 am »
It actually works great. thanks a lot for the hack!