Skip to main content
Inspiring
March 6, 2013
Question

discount voucher how to set up using php

  • March 6, 2013
  • 1 reply
  • 3680 views

what is the best way to setup a discount voucher so when the user inputs a set number provided by us it will give a % discount. and this is pllied to the the total of the shopping cart

i know my question is vague but i dont know where to start

thanks

This topic has been closed for replies.

1 reply

Rob Hecker2
Legend
March 6, 2013

For this kind of feature I use a table with the following fields:

promo_code (the promo or voucher text/number the customer will enter)

promo_deduction (the percentage of the deduction)

promo_expiration (a date you want the offer to end)

promo_id (auto increment primary key)

feature_id (if you want the offer to apply to only a specific product or service, this prevents it being applied to any product/service)

Then, if PHP is performing the calculation, in your POST data you would first check to see if they entered a voucher code, then compare it to your table of valid codes and if there is a match, return the deduction amount as a variable. This is accomplished with a simple WHERE clause in your query  (...WHERE promo_code=:promo_code_entered AND promo_expiration <=:currentdate AND feature_id =:feature_id)

Then multiply the variable by the total, like so:

$total = $pretotal * $promo_deduction;

You could alternatively use Javascript to create a live response, so that when they enter a code in the voucher field, it will update the total or respond with a message that the voucher was not valid.

Inspiring
March 8, 2013

Thanks for that, i will see if i can use your idea and link it in with my existing cart

Participating Frequently
March 25, 2013

can anyone else help me on my discount voucher problem as explained above?

thanks in advance


If you have the database set up with codes and discount already stored the next thing to do is to add the discount form to the cart area, somthing like:

<form id="form1" name="form1" method="post">

      <label for="DiscountCode">Discount Code:</label>

      <input type="text" name="DiscountCode" id="DiscountCode">

      <input name="update" type="hidden" id="update" value="update">

      <input type="submit" name="submit" id="submit" value="Update">

</form>

You'll see i use a hidden field, this is so when you submit this form on the cart page you can use php to start your query only if this form has been submitted

<? if ($update == 'update') {

perform the database query here ...

} ?>

so when this form is used the customer will have entered a promo code, if the text field is blank return an error message, if the promo code is not matched to a database code return an error message, if the code matches a code in the database then you'll return the discount amount and that will be used in your calculations and the $XCart_sumTotal will be reduced by that amount